I know that switch-case is better than if else from a readability point of view but which one is better when it comes to speed and efficiency of processing?
| SitePoint Sponsor |
I know that switch-case is better than if else from a readability point of view but which one is better when it comes to speed and efficiency of processing?
Community Team Advisor
Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


I know that in C/C++ it depends on the amount of cases there are, if there's only a couple it works like an if else, otherwise it works like a jump I believe.
Well, some ol' guru can differentitate between the efficiency of the two, but in practice, the ease of use and semantics rules. Use each where suitable.
And if you are really need to cut such tiny pieces of processing resource, think about getting more hardware power![]()
A better alternative then doing micro-optimizations is to profile your code. What i mean by that that is to run portions of the application and dissect the slow parts.
http://en.wikipedia.org/wiki/Performance_analysis
switch-case is preferred in fixed options and if else is better in checking truth with in a statement which has a large range..
This code:
Executes under 0.2 ms, let me say that again 0.2 ms. That is very very very fast.PHP Code:<?php
function switchblock ( $value )
{
switch ( $value ) {
case 0: return 'zero';
case 1: return 'one';
case 2: return 'two';
case 3: return 'three';
case 4: return 'four';
case 5: return 'five';
case 6: return 'six';
case 7: return 'seven';
case 8: return 'eight';
case 9: return 'nine';
default: return 'whatever';
}
}
function ifelseblock ( $value )
{
if ( $value === 0 ) return 'zero';
elseif ( $value === 1 ) return 'one';
elseif ( $value === 2 ) return 'two';
elseif ( $value === 3 ) return 'three';
elseif ( $value === 4 ) return 'four';
elseif ( $value === 5 ) return 'five';
elseif ( $value === 6 ) return 'six';
elseif ( $value === 7 ) return 'seven';
elseif ( $value === 8 ) return 'eight';
elseif ( $value === 9 ) return 'nine';
else return 'whatever';
}
#-------------------------------------------------------------------------------
for ( $i = 0; $i < 11; $i++ ) {
var_dump( switchblock( $i ), ifelseblock( $i ) );
}
Click to enlarge
![]()

The switch/case can't be slower than a corresponding series of if/else because the worst way that the switch can be treated is as if it were that series of if statements. Unless you have hundreds of thousands of cases though it probably isn't going to make any noticanble difference.
Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">




switch statements aren't really what you need to be optimizing, since the time it takes is negligible. the only time it'll really matter is if you're calling a function in your condition statement.
for example:
in this case the switch would be faster as it only requires one evaluation of the test case, while the if block requires the function to be evaluated every time. this can be avoided easily, though, by counting the array beforehand.PHP Code:if (count($array) == 0){
return "there are no records";
} else if (count($array) == 1){
return "there is only one record";
} else {
return "yay lots of records";
}
switch (count($array)){
case 0: return "there are no records";
case 1: return "there is only one record";
default: return "yay lots of records";
}
also, this principle applies to loops, the first for loop requires the script to count the array every iteration, while the second one counts it just once.
PHP Code:for ($x = 0; $x < count($array); $x++){
echo $x;
}
$numElements = count($array);
for ($y = 0; $y < $numElements; $y++){
echo $y;
}

For 2 options:
1. Theoretically: 'If-else' statement is faster than 'switch' statement.
2. Practically: Both are same to execute.
But the truth is: 'switch' statement is easy to code quickly. If u place 10/15 if-else statements it kills time, but for switch its that easy & fast.

Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">
Bookmarks