1
Independent of language, the switch
has better performance than an alignment of if
s?
If yes: why?
if
...
elif
...
elif
...
else
...
end
switch
case
...
case
...
case
...
else
...
end
1
Independent of language, the switch
has better performance than an alignment of if
s?
If yes: why?
if
...
elif
...
elif
...
else
...
end
switch
case
...
case
...
case
...
else
...
end
2
if a == 0
...
elsif a == 1
...
elsif a == 2
...
else
...
end
It is identical in semantics to:
switch a
case 0
...
case 1
...
case 2
...
default
...
end
Thus, a good tool will generate the same code or equivalent code in any measurable sense. If this is not true for your specific case (perhaps some feature of the language), then this case has to be seen in particular.
The only clear exception of the rule is whether your a
not a simple expression, like a variable reading, but something complex that involves executing code. For example:
if prod.getProductType() == 50
...
elsif prod.getProductType() == 78
...
end
It can clearly perform the function more than once, whereas in the case of the switch this would not happen. (Of course, an optimization can conclude that the function is pure and call only once, but this is not the case). If you need to do some computation to get the value you are going to test, better use switch. Otherwise, the two are equivalent. Use the one you find most readable.
One point to consider is that transforming a switch into a jump table (more efficient) is much easier to do by the compiler than from ifs. Therefore, if there are many cases and possibilities, it may be more advantageous to use a switch. Note that this also depends on the language.
Browser other questions tagged performance if switch
You are not signed in. Login or sign up in order to post.
Unfortunately I do not remember where I read that one should avoid ELSEIF whenever possible because during the interpretation of the code each of the conditions is tested before. However, in my view, it is impossible to answer this kind of question without knowing every language in depth.
– Bruno Augusto
There is not much difference to tell the truth, the performance is similar. Take a look at this link: If Or Switch
– f.fujihara
"Language independent" is difficult to answer because it depends on how it implements the
switch
. Her could compare the argument with eachcase
, she could "stack" the argument (if it is a stack architecture) and compare "the top of the stack" with each case, it could use a lookup table... To my knowledge, in most common languages the difference in performance is small or negligible, but I cannot say for sure without: a) testing; b) checking the implementation (i.e. seeing how machine code is generated).– mgibsonbr
What language are you talking about? This cannot be answered for sure for all languages. Only assumptions can be made. Would it be Python? So far I don’t like the answers posted. Some even make simplistic assumptions and flirt with the error.
– Maniero
There is a technical way to explain this ... ?
– user6026
@Harrypotter Benchmarks.
– Guilherme Oderdenge
@Guillermordenge In all languages?
– Maniero
@bigown If we stop to think, the technical way to explain performance in the middle of technologies is with benchmarks - no language follows a logical performance flow, so if we benchmark all languages then yes, we have a technical explanation for all of them.
– Guilherme Oderdenge
@Guilhermeoderdenge I agree. There seems to be not enough space here for all of them.
– Maniero
An idea then would be to ask a question for each language because here it would be very broad
– user6026
Javascript does not have Elif, end, hit the syntax then. But I do not know how it will look all the answers that were given without knowing it was JS. @Bacco also knew nestled for this. If you find something, share the result :)
– Maniero
I’ve always called this the "chain of ifs"... In fact, although the code is not nested, the semantics is equivalent to a nesting within the
else
. Example:if { ... } else { if { ... } else { if { ... } else { ... } } }
– mgibsonbr
@Bacco I meant "nestled" even, because it is equivalent to the code that mgibsonbr posted.
– user7261
@Bacchus
1)
The "L" was a typo. The fact that they aligned with theelif
was coincidence.2)
I’m quite calm, I don’t know how you thought I wouldn’t. hahaha =)– user7261