On Thursday, 18 April 2013 at 14:26:08 UTC, deadalnix wrote:
On Thursday, 18 April 2013 at 13:58:46 UTC, Idan Arye wrote:
Naturally - even as statements, a `switch` statement without a switch expression is no better than a chain of `if`-`else`s.


As mentioned before in other thread, LLVM is crazy good at transforming if/else chains into switch statement, and GCC does a good job at it as well.

A `switch` statement is faster than a `if`-`else` chain only when it can use a lookup table, and for the lookup table a switch expression is a must. Another precondition for a lookup table is that the test expressions are known at compile-time.

The given example has none - there is no switch expression(though one may argue that you can use `true` as the switch expression) and the test expressions are function calls calculated at runtime.

If the compiler can translate a `if`-`else` chain to a `switch` statement - then it should have probably been a `switch` statement in the first place. That way it would have also been more readable.

Reply via email to