Re: switch statement
- Posted by mattlewis (admin) Mar 26, 2009
- 1342 views
Not all if-elsif-else statements can be optimized this way (think side effectsbad practice, but still needs to be taken into account), but your statement is just referring to a trivial isomorphism between a switch and if-elsif-else blocks. By which I mean that all switches (that don't use fallthrough) can be trivially altered to be an if-elsif-else.
Well, if the compiler does common subexpression elimination, it is easy to optimize an if-elif-chain into a jump table. Speed is no justification for a switch/select statement nowadays, if you are going to have a proper optimizer anyway. But please get me right: I love "select"-statements. It's just that speed is no good argument to introduce them.
Yes, for certain if-elsif-else scenarios it's easy, which is exactly what I said (trivial isomorphism, etc, etc).
Speed is never a good reason to do anything, until you need it. And then it's a good thing.
Matt