Performance: Switch or if aligned?

Asked

Viewed 1,128 times

1

Independent of language, the switch has better performance than an alignment of ifs?

If yes: why?

if
  ...
elif
  ...
elif
  ...
else
  ...
end

switch
  case
    ...
  case
    ...
  case
    ...
  else
    ...
end
  • 2

    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.

  • There is not much difference to tell the truth, the performance is similar. Take a look at this link: If Or Switch

  • 6

    "Language independent" is difficult to answer because it depends on how it implements the switch. Her could compare the argument with each case, 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).

  • 1

    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.

  • 1

    There is a technical way to explain this ... ?

  • @Harrypotter Benchmarks.

  • @Guillermordenge In all languages?

  • @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.

  • @Guilhermeoderdenge I agree. There seems to be not enough space here for all of them.

  • An idea then would be to ask a question for each language because here it would be very broad

  • 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 :)

  • 2

    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 { ... } } }

  • @Bacco I meant "nestled" even, because it is equivalent to the code that mgibsonbr posted.

  • @Bacchus 1) The "L" was a typo. The fact that they aligned with the elif was coincidence. 2) I’m quite calm, I don’t know how you thought I wouldn’t. hahaha =)

Show 9 more comments

1 answer

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

You are not signed in. Login or sign up in order to post.