Click to See Complete Forum and Search --> : Efficiency of switch statement.


xargon
February 21st, 2003, 09:31 AM
Hi all,

Just a general question. I heard today that the switch statement in C++ is more efficient than using if-elseif structure. Someone told me that the order in which the 'case' statements appear is not important anymore. IS this true? Does C++ create some kind of a jump table for a switch statement or something?

Thanks,

Xargon

KevinHall
February 21st, 2003, 11:05 AM
Implementations are compiler dependent, but yes compilers can often optimize switch statements better than if-else if-else blocks. In a worst case scenario a case statement should perform equally with if-else if-else blocks. However, since teh compilation of switch statements is compiler dependent (and can sometimes be reduced to if-else if-else blocks) then the order of cases can matter. If you are concerned about a particular example, profile it!

Hope this helps!

- Kevin

galathaea
February 21st, 2003, 12:23 PM
We had a nice discussion (http://www.codeguru.com/forum/showthread.php?s=&threadid=225285&highlight=switch) that looked over this a little while back on these forums. It was discovered that the compilers we looked at often did create a jump table when the number of switch statements exceeded some lower bound. However, as the thread explores, there are faster methods available as well. And in comparison to if's, I think you will need to doyour own profiling and checking of the assembly to see, but my hypothesis in that thread was that the ifs would be taken care of at the same level of optimization as the switch if the arguments are the same, since I would think that a good compiler would perform this translation on the intermediate code phase and not during semantic translation, but I never checked it out...

Gabriel Fleseriu
February 21st, 2003, 12:36 PM
As Kevin already said, the way a specific compiler translates (into machine code) a specific switch cannot be predicted in general. It is implementation dependent. However, understanding what optimizing possibilities a compiler has can help writing faster switch statements. One obvious thing is that if the case constants are (nearly) successive (case 1:, case 2:, case 3:...) -- as opposed to "random" (case 1:, case 123456:, case -123:, ...) -- the compiler has a better chance to build up a jump table. This does not mean that it has to produce better code, it means that it could produce better code.

On the other hand, I have to stress out what Kevin already correctly stated: do not optimize if you haven't profiled.

Also note that the decision to use a switch() or if/else if/.... can massively improve your code readability, debugging time and maintenance costs. The same is true for organizing switch() statements: it is rarely worth to build up a switch that is tuned for raw performance and thus is constructed in terms of the programming language instead of being contructed in terms of the problem to solve.

Someone once said (I think it was Scott Meyers, not sure, though):
The two rules of optimizing are:
1. Don't do it.
2. Don't do it yet.

:)