Which is faster? switch or if-else ?
Every time I have a state where I need to use switch\if-else-irritation I can’t quite figure our which is the better choice. I am pretty sure there is no one answer to this question, but i did stumble upon an interesting test:
This test shows that, as i suspected all along(Mu-ha-ha), the switch is faster, and in a significant way.
I’m sure that some people will still state if-else is more readable, but personally, i do not agree with that.
What do you choose when you face this issue? if-else/switch? or an entirely different mechanism?
Have a good one, =]
Nadav
Tags: c#, coding, conventions, else, if, if-else, programming, switch
February 12th, 2008 at 1:44 am
If you’re wondering what’s the reason switch is faster, it is because it can determine the case which the application should jump into really fast (using a hash table). If/else always runs through all of the tests.
February 17th, 2008 at 7:04 am
Nice test
March 26th, 2008 at 8:24 pm
If you’re talking about an interpreted language, I would probably say switch statements. However, any good compiler would produce the exact same code either way (or should).
March 26th, 2008 at 8:49 pm
The difference in speed is negligible and highly dependent on the language implementation. Some languages don’t have switches at all. Some languages have switch statements which are broken in the sense that forgetting a break statement will cause you to go on a wild goose chase to track down a stupid programming error. Some languages are slow no matter which you use! Caching and the number of cases can affect the performance greatly.
It’s best not to worry about infinitesimal performance deltas in programming. If it really mattered, you’d use assembly instead of C#.
March 26th, 2008 at 8:59 pm
I wonder if polymorphism would be another approach… especially if you have various calls based on your conditions…
March 27th, 2008 at 5:55 pm
How about “most often case”… say I loop a number of times over [1,2,3,4,5]. What sould be faster?
switch(a % 3){
case 0:
case 1:
case 2:
}
if(a % 3 == 2) {} else if( a % 3 == 1) {} else {}
or
if(a % 3 == 0) {} else if(a % 3 == 1) {} else {}
The first if (yes, it’s an example made specifically for this, but there are cases where such a construction is useful) should be faster than the first; there are twice as many numbers that have remainder 1 and 2 (1 and 4, and 2 and 5, respectively) than 0 remainder (3). The first if should be a little bit faster than the second, because the most common cases are first. On the switch, however, the mod is calculated only once.
It all depends on the problem. Sometimes an if is faster.
March 29th, 2008 at 4:05 pm
Some interesting comments. The speed test is C# specific so if you need to know which is faster in other languages you need to try it out. The switch statement is faster because the compiler optimises it differently to if-then-else, specifically ordering the cases to allow a fast lookup.
The speed improvement is small for single instances but if this type of statement appears within a large loop, the performance gains can be significant.
Finally, thanks for the link