0

Possible Duplicate:
If vs. Switch Speed

I will use c++ as an example, but the question I am asking isn't towards a particular language. I mean in general. Which one is better for performance? This isn't a question about programming styles.

Of course, in certain circumstances the switch statement may be easier to use than an if statement. But if you wanted MAXIMUM performance. Like if there was thousands of lines of code and more than 10 switch statements that COULD be replaced with if statements, which one would cause better performance?

Example:

if(x == 6){
do this
}

if(x == 67){
do this * 2
}

if(x == 9000){
do this * 3
}

vs

//Please forgive me if the syntax for the switch statement is wrong, I haven't use one in any programming language for a long time
Switch(x){
case 6:
    do this

case 67:
    do this * 2

case 9000:
    do this * 3
}

So how does this differ in performance? When you answer, make sure your answer includes the programming language you are referring to. If in java, state that you are talking about java and not c++, javascript, whatever language you chose to answer my question.

Community
  • 1
  • 1
Gabriel
  • 3,039
  • 6
  • 34
  • 44
  • Why don't you run a benchmark for yourself? –  Nov 30 '11 at 15:26
  • 1
    I'm pretty sure that, whatever the language is, you won't have any noticeable difference between both, even if invoked a billion times. Use what is the more readable and maintainable. – JB Nizet Nov 30 '11 at 15:27
  • 2
    Be sure to include `break;`s in the `switch` or the codes won't be equivalent. – Toomai Nov 30 '11 at 15:27
  • Is this in pseudo code or did you intend to have code that evaluates all the `if` statements and executes all `cases` in the `switch` – r_ahlskog Nov 30 '11 at 15:28
  • @Toomai thats not the point... its just the idea of a switch vs an if. – Gabriel Nov 30 '11 at 15:28
  • How could anyone possibly answer the question without even knowing the language you're targetting? – Puppy Nov 30 '11 at 15:41
  • While the answers belwo are good, in fact you can not answer this in general. Depending on compiler and its optimizations and also the processor architecture and most important the size of the if chain your code might behave completely differen. – Angel O'Sphere Nov 30 '11 at 16:22

5 Answers5

4

Switch may (depending on the implementatin) use jump table (in c and c++), so under certain conditions it may be more performant. Generally, it states your intention more clear so compiler can pretend it's smart.

considering if in your example doesn't even use else if chain, it makes switch even faster (because of not evaluating all conditions) and also may change the meaning of your code (in case your dothises modify x).

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

Well, as stated here: If vs. Switch Speed and here: Advantage of switch over if-else statement

So actually switch is faster than a lot of chained if-else statements under dertain circumstances due to the compiler being able to optimize a bit.

Community
  • 1
  • 1
Pieter
  • 3,339
  • 5
  • 30
  • 63
1

With C++ the switch statement is faster to execute than multiple if statements. This is because the compiler is able to optimise the switch statement but with multiple if's the code must process each if statement in the order set by you the programmer. With switch statements, it doesn't rely on earlier cases so the compiler is able to re-order in a way making it faster to execute.

litterbugkid
  • 3,534
  • 7
  • 36
  • 54
1

Generally if you have a list of very specific values/actions either will do. Switch is likely somewhat faster in most cases due to the ability to create jump tables from the known compile time values. However, there are some specific situations in which you can use a switch statement to specifically increase performance, for instance Duff's Device.

Chad
  • 18,706
  • 4
  • 46
  • 63
  • Modern compilers can produce better code than that created using Duff's Device, and is far more readable. Don't forget that unrolling loops increases code size which can also slow the CPU down (more memory reads for the instructions). – Skizz Nov 30 '11 at 15:32
1

According to this benchmark a switch statement is faster.

http://jsperf.com/switch-vs-if3

brenjt
  • 15,997
  • 13
  • 77
  • 118