At 21:02 18.03.2003, Liam Gibbs said: --------------------[snip]-------------------- >Is it just as quick to do: > >if($r == 0) { >} else if($r != 0) { >} > >than to do: > >if($r == 0) { >} else { >} > >The reason I like the former method is because, in a large IF-ELSE block, >it's clear what belongs to what IF and what's going on. But does this make >it lag? And, if so, is it really all that noticeable? --------------------[snip]--------------------
Noticeable? Probably not, except you're timing loops of a million or more cycles... but it's clear that the second method needs more time (in terms of cpu cycles) than the first one. In Intel speak, what would a compiler translate these code examples to? if (a == b) {} else {} mov eax, {a} cmp eax, {b} jnz L01 ; the "true" block goes here jmp L02 L01: ; the "false" (else) block goes here L02: As you can see there's two memory operations and one comparison. Your second example: if (a == b) {} else if (a != b) {} mov eax, {a} cmp eax, {b} jnz L01 ; the "true" block goes here jmp L02 L01: mov eax, {a} cmp eax, {b} jz L02 ; the second block goes here L02: There's four memory operations, and two comparisons. As another poster already pointed out, there's also a slight difference comparing something to zero, or to false: if (a == 0) mov eax, {a} cmp eax, 0 jnz L01 if (!a) mov eax, {a} or eax, eax jnz L01 The latter version consumes approx. half of the time (in cpu cycles) compared to the first one. Modern compilers will leverage the need for a to deal with these efforts. The multi-pass optimizers usually detect situations like these and would use the "if (!a) {} else {}" construct, without regard what your original input is. However, PHP is _not_ a compiler, it's an interpreter. It "compiles" code at runtime and has not the ability to perform exhaustive optimizations which would cost far more time than can be gained within a single execution. Additionally there's the time needed to parse the input stream (tokenize, add to symbol table, execute token and expression parser, etc etc). With interpreting languages the differences will rocket skyhigh compared to compiled executables. Well, it's still to be measured in nanoseconds for a single instance, thanks to modern processor power... -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php