Are there any performance differences between using if-else and case statements when handling multiple conditions?
Which is preferred?
Are there any performance differences between using if-else and case statements when handling multiple conditions?
Which is preferred?
In some languages, like C, switch
may possibly be faster because it's usually implemented with a jump table. Modern compilers sometimes are smart enough to use one for several if
s as well, though.
And anyway it probably won't matter, micro optimizations are (almost) never useful.
Some programming languages restrict when you can use switch
/case
statements. For example, in many C-like languages the case
values must be constant integers known at compile time.
Performance characteristics may differ between the two techniques, but you're unlikely to be able to predict in advance what they are. If performance is really critical for this code in your application, make sure you profile both approaches before deciding, as the answers may surprise you.
Normally, performance differences will be negligible, and you should therefore choose the most readable, understandable and maintainable code, as you should in pretty much any other programming situation.
When you have more than one ifelse, I recommend switch/select. Then again, it doesn't always work.
Suppose you something like that (not recommended but for example only)
If a > 0 and b > 0 then
' blabla
ElseIf b = -5 then
' blabla2
ElseIf a = -3 and b = 6 then
End If
Using a switch/select is NOT the way to go. However, when querying for a specific value for a variable like this
select case a
case 1:
' blabla
case 2:
' blabla2
case 3:
' blabla3
case 4:
' blabla4
case else:
end select
In those case, I highly recommend it because it is more readable for other people.
I will add to some of the answers here. This is not a performance question, and if you are really concerned about performance... write a test and see which is faster.
However, this should be a question about which is proper to use, not which is better. If you have multiple if/else statements then do yourself a favor and use a case statement. If it is a simple if/else then use an if/else. You'll thank yourself later.
case
or switch
statements are really just special cases of "if .. elseif..." structures that you can use when the same object is being compared to a different simple value in every branch, and that is all that is being done. The nice thing about using them is that most compilers can implement them as jump tables, so effectively the entire 200 (or however many) branch checks can be implemented as a single table indexing operation, and a jump.
For that reason, you'd want to use a case
statement when you
The larger the number of "elseif"s, the more attractive a case
statement is.
Case statements are generally preferred for readability and are generally faster if there is any speed difference, but this does not apply to every possible environment.
You could probably write a test that shows which is faster for your environment, but be careful with caching and compiler optimizations.