if/elseif (===) vs switch/case, faster?

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
rxsid
Forum Commoner
Posts: 82
Joined: Thu Aug 29, 2002 12:04 am

if/elseif (===) vs switch/case, faster?

Post by rxsid »

Hi all,

I need to run a check within a for loop (which could be up to 30,000 iterations ($count)). Which would be faster & why?

Code: Select all

for ($x=0;$x<$count;$x++) {
  if ($item === "$a") {

  } elseif ($item === "$b") {

  } elseif ($item === "$c") {

  }  17 more time (20 total)
}
OR

Code: Select all

for ($x=0;$x<$count;$x++) {
  switch($item) {
     case '$a'; break;
     case '$b'; break;
     case '$c'; break;
     17 more as above
  }
}
between if/elseif's and case/breaks i would do "something."

....wondering if anyone would know offhand if one or the other would be faster, or has done some benchmarking regarding this type of question.

Thanks!
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

And the winner...

Post by trollll »

Switch/case runs a bit faster. I researched this about a year ago and have a rather inconsistant memory, so somebody please correct me if I have the reason incorrect.

When you have several options set up as

Code: Select all

for ($x=0;$x<$count;$x++) &#123;
  if ($item === "$a") &#123;

  &#125; elseif ($item === "$b") &#123;

  &#125; elseif ($item === "$c") &#123;

  &#125;  17 more time (20 total)
&#125;
it has to chug through each comparison until it finds one that returns true.

However, when you have several options set up as

Code: Select all

for ($x=0;$x<$count;$x++) &#123;
  switch($item) &#123;
     case '$a'; break;
     case '$b'; break;
     case '$c'; break;
     17 more as above
  &#125;
&#125;
from what I understand it goes directly to the matching case.

I think the "if...else if..." method compares to searching for a phrase in a non-indexed site while "switch...case...case..." method compares to searching for a phrase in an indexed site.
void
Forum Newbie
Posts: 3
Joined: Sat Jun 14, 2003 5:21 pm

Post by void »

You can use break in a for structure as well. But I would use switch, it looks better :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Code: Select all

<?php
function getmicrotime(){
    
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}

echo '<p><b>TEST</b></p>';

$start = getmicrotime();

// code to test

$end = getmicrotime();
$time = $end - $start;
echo '<p>time = ' . $time . '</p>';

?>
Last edited by McGruff on Thu Aug 11, 2005 5:34 am, edited 1 time in total.
scH
Forum Newbie
Posts: 5
Joined: Mon Jun 16, 2003 11:48 am
Contact:

Post by scH »

I don't think you'll notice a whole lot of speed either way, but the switch statement will be a hell of a lot easier to maintain and add new cases to.
rashed
Forum Newbie
Posts: 17
Joined: Fri Jun 06, 2003 6:27 am
Location: Islamabad, Pakistan.

Post by rashed »

It depends upon the operations like comarison operation,assignment and function call etc etc.
SWITCH statement is advanced form of IF statement and very much easy to understand and implement.
Both have same number of operations. Both implement jump statement etc. etc.
bjg
Forum Newbie
Posts: 15
Joined: Tue Jun 10, 2003 9:42 am
Location: .au

Post by bjg »

I just did some benchmarks using 'time' with bash. switch was slower than if surprisngly. But of course it's not conclusive, they were only simple tests on one platform. Plus 'time' is a built in bash tool.

It's up to you on what you want to do, but personally i'd choose switch over if for your situation, only because it's neater and more maintainable.
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

Switch is faster.

I would advise to use switch when you are comparing against 1 value and have more than a few comparisons to do.
The speed difference won't be massive until the amount of comparisons mounts up, but if you have 20 comparisons inside a loop, then the speed difference will be noticeable.

Kelvin
bjg
Forum Newbie
Posts: 15
Joined: Tue Jun 10, 2003 9:42 am
Location: .au

Post by bjg »

releasedj wrote:Switch is faster.
How do you know this? Just curious.
kenankara.com
Forum Newbie
Posts: 3
Joined: Tue Jun 17, 2003 8:02 am
Contact:

Post by kenankara.com »

else if tag has an upper limitation,
so that i suggest u to use switch / case statements.

http://www.kenankara.com
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

There is more overhead involved with doing if statements as they get evaluated each time, whereas a switch statement is only evaluated once.

Here's a quote from the php manual:
In a switch statement, the condition is evaluated only once and the result is compared to each case statement. In an elseif statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a switch may be faster.
Kelvin
rashed
Forum Newbie
Posts: 17
Joined: Fri Jun 06, 2003 6:27 am
Location: Islamabad, Pakistan.

Post by rashed »

It means putting a value in variable befor all IFs and then testing using IF statements is same as using SWITCH construct?
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

I doubt it, I think that it is still probably quicker because PHP has to do a lot more to deal with the if/elseif syntax than switch.

i.e. in a switch, once PHP has evaluated the expression, it looks for a case statement that matches the value, that's all. In an if statement, once it has evaluated the expression, it then has to be ready for any further expressions to evaluate to decide whether or not to execute this block of code. This happens on each if/elseif statement.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Why don't you time your script with each method and report back?
Post Reply