Many if statements faster then elseif

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Many if statements faster then elseif

Post 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';
?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

No... Maybe... a consistent fluke? :?

PHP is reliant on the server and the browser so... Any of those could be the cause.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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. ;)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply