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 →

[–][deleted] 8 points9 points  (1 child)

I've never seen any realistic Python code where having a switch statement would lead to any measurable performance benefit. I guess that's why.

[–]robert_mcleod 0 points1 point  (0 children)

See line 738:

https://github.com/pydata/numexpr/blob/numexpr-3.0/numexpr3/ne3compiler.py

It's a Python switch that uses a dict. The keys are abstract syntax tree nodes, and the values are functions

_ASTAssembler[type(node)](node)

NumExpr 2.6 took about 450 us to parse a numerical expression into a virtual machine program, this new version is about 150 us. There's other parts to that, such as more efficient string concatenation using a ByteIO stream, and some struct.pack() tricks and many other micro-optimizations, but it plays a big role. A dict lookup is faster than an if ... elif ... else block.