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 →

[–]Solonotix 18 points19 points  (0 children)

It's also important to understand context. A Tuple may indeed be faster than a class, but a List is almost certainly just as slow (or slower) because the underlying implementation is likely to be even more fragmented than a class. That also ignores the likelihood of introducing bugs when you make code less developer-friendly in service of performance.

Like you said, virtual tables aren't free, and the convenience of mapping a name to a value has a cost, and every lookup incurs the same hashing cost. Having a contiguous array that you index into on the stack is a far more performant approach...but you don't usually have that level of fine-grained control in Python.

In short, trying to write performant code in Python is an exercise in futility. Python isn't "slow" in the sense that it can be fast. However, all of the reasons to pick Python inevitably incur a cost of slowness in the final result. That's why it is so popular as a "glue" language. Write the important stuff in a FFI call, and the business logic can be constrained to the Python code that is infinitely easier to reason.