Page 1 of 1
if/elseif (===) vs switch/case, faster?
Posted: Mon Jun 16, 2003 3:29 pm
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!
And the winner...
Posted: Mon Jun 16, 2003 4:42 pm
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++) {
if ($item === "$a") {
} elseif ($item === "$b") {
} elseif ($item === "$c") {
} 17 more time (20 total)
}
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++) {
switch($item) {
case '$a'; break;
case '$b'; break;
case '$c'; break;
17 more as above
}
}
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.
Posted: Mon Jun 16, 2003 5:12 pm
by void
You can use break in a for structure as well. But I would use switch, it looks better

Posted: Mon Jun 16, 2003 8:10 pm
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>';
?>
Posted: Mon Jun 16, 2003 9:56 pm
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.
Posted: Mon Jun 16, 2003 11:50 pm
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.
Posted: Tue Jun 17, 2003 2:03 am
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.
Posted: Tue Jun 17, 2003 6:35 am
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
Posted: Tue Jun 17, 2003 7:39 am
by bjg
releasedj wrote:Switch is faster.
How do you know this? Just curious.
Posted: Tue Jun 17, 2003 8:02 am
by kenankara.com
else if tag has an upper limitation,
so that i suggest u to use switch / case statements.
http://www.kenankara.com
Posted: Tue Jun 17, 2003 8:07 am
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
Posted: Tue Jun 17, 2003 8:26 am
by rashed
It means putting a value in variable befor all IFs and then testing using IF statements is same as using SWITCH construct?
Posted: Tue Jun 17, 2003 8:31 am
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.
Posted: Tue Jun 17, 2003 8:36 am
by McGruff
Why don't you time your script with each method and report back?