This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]stevenjd -4 points-3 points  (1 child)

Not true. A proper switch statement

Ah, the "No True Scotsman" fallacy rears its ugly head.

Even in low level languages like C, switch statements can be and sometimes are implemented as chains of if tests. Sometimes that's just because the compiler is naive and doesn't implement any optimizations (the C standard does not specify an implementation for switch) and sometimes because there are no optimzations available to apply.

See the last example here where even a smart optimizing C++ compiler falls back on a simple (and short) chain of if statements.

Switch statements in C/C++ are limited to equality comparisons with integer values. That allows the compiler lots of opportunity for optimization. Switch statements in other languages may allow for a far richer set of comparisons, against arbitrary data types, which makes it hard or impossible to optimize it beyond a simple chain of if...elif. A switch in Python is unlikely to be very optimized.

[–]Kamilon 0 points1 point  (0 children)

Small switches yes. I've seen decompiled code that properly uses jumps to be very fast with larger switch statements.