Re: switch statement
- Posted by jeremy (admin) Mar 26, 2009
- 1350 views
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.
Um, speed was not at all the reason for implementing switch! In fact, when it was first introduced and decided on it was slower. I think in this thread you have had several examples of where a switch is less error prone than the current if/else syntax and more featureful due to fall-thru. Adding switch was never about speed nor optimizations. Many optimizations will be coming once we hit beta and further in 4.1.
I will say it again, because you might have missed it, but switch was decided to be added due to it being able to reduce the amount of work on the programmer. Fall thru was decided upon because it's the defacto standard for a switch statement, it's more powerful and we already have the capability of doing non-fall thru decisions.
Jeremy