News for hackers. Stuff that matters.
Re:How the CPython compiler works by ir123 on Thursday September 24, 2020 @12:06PM (#24580037)
Noob question probably, but wouldn't an Enum Map or something similar be more efficient?
Re:How the CPython compiler works by anon946 on Thursday September 24, 2020 @12:47PM (#24580603)
For more than a few cases in the switch:

If the case values are dense (such as just the integers from 0 to 9), the compiler will turn it into a jump table.

https://godbolt.org/z/1Khahr

If too sparse for jump table, then typically will be turned into a binary search tree.

Re:How the CPython compiler works by hn_acc_2 on Thursday September 24, 2020 @12:31PM (#24580387)
It depends on your compiler.

But as a general rule, the programmer will have a hard time "outsmarting" the compiler by using different objects or abstractions.

Compiler authors have spent many hundreds of combined hours making C code run as fast as possible, and optimizing the switch statement is something they've most certainly done to death.

I.e. if your control flow is switch-case-like, use a switch-case.

Re:How the CPython compiler works by Jasper_ on Thursday September 24, 2020 @01:14PM (#24580938)
Not always. Computed gotos can be faster, because it can relax some restrictions that the switch statement can't amke. Most VM implementations, including CPython, use computed gotos for a not insignificant performance improvement.

https://github.com/python/cpython/blob/master/Python/ceval.c...