In the past week, I have been looking more in-depth into the CPython implementation and its bytecode, and I have been very surprised that it’s not performing almost any optimization on the code.
This article is showing exactly this behavior with some trivial examples, and comparing the bytecode compiler of Python, Lua, and Emacs Lisp. All of them don’t do much optimization on the code, and apparently, for CPython, this is a decision they made “by design”.
Guido van Rossum himself said, “Python is about having the simplest, dumbest compiler imaginable.” So this is all very much by design.
This means that if you care at all about performance in Python, you need to be very careful about what code you write and how it’s going to be translated into bytecode. To translate this, it means that if you write a more readable (by humans) code, it’s probably going to be slower.
This is the very opposite of what I was used in C, where the compiler tries to optimize as much as possible your code, so even if you write very human-readable code with multiple variables and expressiveness, the compiler will automatically optimize it, while here it’s the exact opposite.