timvw wrote:You might want to take into consideration that a boolean can only take two values.. Which makes it useless for situations with more than two values
Perhaps...but 95% of record sets I see displayed follow a pattern of typically 2 or none...rarely do I see 3 or more different colored rows...so bitwise works just fine
timvw wrote:
If the number of situations is a power of two, adding a bit each cycle will probably be the most efficient... In other situations you'll really have to compare the index and maximum...
Huh...adding a bit? Why would you need to add a bit? You use a normally stepped counter (1,2,3,4 and so on) using the ++ operator or whatever method you choose. All odd numbers have the first bit set...all even don't...I fail to see your point?
I think what your recalling is that on 80x86 machines it was an assembly trick that shifting left or right would either divide or multiply by two. In which case mult/div operations on numbers of the power of two were faster than using the actual arithmetic instructions. A bitwise operation like I've suggested above...works regardless of whether the number is a power of two or not. It simply checks the first bit of a byte for ON/OFF state...yes it's boolean, but I like said, how many applications do you know of which use more than 2 colors? I can't list (m)any
timvw wrote:
The last time i looked it up in the instruction set a modulo operation was more expensive than a compare and add operation.. But that's at the level of instruction sets... For use in a PHP environment the performance differences would most certainly not be the reason i choose one over the other...
Yes it's at the level of instructions sets...but that knowledge is what makes you an experienced developer and lets you make decisions which further enhance your code...anyone who would argue that a simple bitwise check is more difficult than modulus...I dunno...maybe they should be programming...cause as long as you understand how binary works...it makes total sense.
I wouldn't go out of my way to optimize my code, but little caveats, like single quotes over double quotes...it's a split second decision which I either accept or refuse. Alone maybe it's nothing, but accumulated over a period of 30 days on a high use web site...those little tricks add up
Cheers
