Page 1 of 1

Many if statements faster then elseif

Posted: Wed May 30, 2007 5:54 pm
by WaldoMonster
I was really surprised with this result.
I thought that with this code after the second elseif statement the rest of the elseif statements don't have to be evaluated and therefore faster that the second script.
But this is not the case.
Can someone explain why the second script is faster?

Code: Select all

<?php
list($usec, $sec) = explode(' ', microtime());
$start_time = $usec + $sec;

$q = 2;
for ($i = 0; $i < 1000000; $i++)
    {
    if ($q == 1) {}
    elseif ($q == 2) {}
    elseif ($q == 3) {}
    elseif ($q == 4) {}
    elseif ($q == 5) {}
    elseif ($q == 6) {}
    elseif ($q == 7) {}
    elseif ($q ==  {}
    elseif ($q == 9) {}
	}

list($usec, $sec) = explode(' ', microtime());
$execution_time = $usec + $sec - $start_time;
echo 'Script execution time: ' . number_format($execution_time * 1000, 1) . 'ms';
?>

Code: Select all

<?php
list($usec, $sec) = explode(' ', microtime());
$start_time = $usec + $sec;

$q = 2;
for ($i = 0; $i < 1000000; $i++)
    {
    if ($q == 1) {}
    if ($q == 2) {}
    if ($q == 3) {}
    if ($q == 4) {}
    if ($q == 5) {}
    if ($q == 6) {}
    if ($q == 7) {}
    if ($q ==  {}
    if ($q == 9) {}
	}

list($usec, $sec) = explode(' ', microtime());
$execution_time = $usec + $sec - $start_time;
echo 'Script execution time: ' . number_format($execution_time * 1000, 1) . 'ms';
?>

Posted: Wed May 30, 2007 6:42 pm
by superdezign
No... Maybe... a consistent fluke? :?

PHP is reliant on the server and the browser so... Any of those could be the cause.

Posted: Wed May 30, 2007 6:52 pm
by RobertGonzalez
Of course those of that have mastered the use of the switch statement typically do not test the boundaries of a bunch of if's. ;)

Posted: Wed May 30, 2007 7:01 pm
by superdezign
Everah wrote:Of course those of that have mastered the use of the switch statement typically do not test the boundaries of a bunch of if's. ;)
:lol: Yeah! Hey WaldoMonster! I bet you my switch statement is faster than your multiple if conditionals!

Posted: Wed May 30, 2007 8:39 pm
by s.dot
My test shows that the first script is noticeably faster; opposite of your test.

Code: Select all

C:\Users\HP_Administrator>php -r "$start = microtime(true); $q = 2; for($i=0;$i<
1000000;$i++){ if($q == 1){} elseif($q == 2){} elseif($q == 3){} elseif($q == 4)
{} elseif($q == 5){} elseif($q == 6){} elseif($q == 7){} elseif($q == 8){} elsei
f($q == 9){} } $end = microtime(true); echo $end-$start;"

0.28984498977661

C:\Users\HP_Administrator>php -r "$start = microtime(true); $q = 2; for($i=0;$i<
1000000;$i++){ if($q == 1){} if($q == 2){} if($q == 3){} if($q == 4){} if($q ==
5){} if($q == 6){} if($q == 7){} if($q == 8){} if($q == 9){} } $end = microtime(
true);  echo $end-$start;"

0.82610106468201

C:\Users\HP_Administrator>

Posted: Wed May 30, 2007 8:47 pm
by superdezign
scottayy wrote:My test shows that the first script is noticeably faster; opposite of your test.

Code: Select all

C:\Users\HP_Administrator>php -r "$start = microtime(true); $q = 2; for($i=0;$i<
1000000;$i++){ if($q == 1){} elseif($q == 2){} elseif($q == 3){} elseif($q == 4)
{} elseif($q == 5){} elseif($q == 6){} elseif($q == 7){} elseif($q == 8){} elsei
f($q == 9){} } $end = microtime(true); echo $end-$start;"

0.28984498977661

C:\Users\HP_Administrator>php -r "$start = microtime(true); $q = 2; for($i=0;$i<
1000000;$i++){ if($q == 1){} if($q == 2){} if($q == 3){} if($q == 4){} if($q ==
5){} if($q == 6){} if($q == 7){} if($q == 8){} if($q == 9){} } $end = microtime(
true);  echo $end-$start;"

0.82610106468201

C:\Users\HP_Administrator>
THAT makes sense.
There you go. Cutting out the middleman of the browser and such.

Posted: Thu May 31, 2007 4:15 am
by WaldoMonster
I was one again forgotten to disable the eAccelerator.
With eAccelerator disabled the execution time is the other way around.
I also have made a comparison with switch.
You can see that elseif and switch are not to different,
if statements are now much slower.

Code: Select all

q = 2;                 q = 9;               
------------------------------------------------
switch           870 ms                 1370 ms
switch + break   765 ms                 1400 ms 
elseif           885 ms                  875 ms
if              2325 ms                 2390 ms
Update: here are the results with eAccelerator enabled.

Code: Select all

eAccelerator     q = 2;                 q = 9;               
------------------------------------------------
switch           700 ms                 1110 ms
switch + break   695 ms                 1110 ms 
elseif           805 ms                 1700 ms
if               340 ms                  345 ms

Posted: Thu May 31, 2007 7:39 am
by superdezign
Well, my first reaction was "then your eAcceleraor sucks!" (causing my switch to be slow... pssh! :lol:) until I realized that, with it, everything is faster. Although, I'd never think that an accelerator would work backwards like that and make you have to change your coding style for maximum efficiency.