Performance optimizations in Python 3.11
Python 3.11 will be 10-60% faster than Python 3.10. On average, the increase in productivity will be 25%. What makes Python faster?
Quick launch
Python uses the __pycache__
folder to cache modules for fast loading. If previously in Python 3.10 the process of executing a module from the cache took 3 steps, Python 3.11 managed to reduce this process to one step.
Now starting the interpreter will be 10-15% faster, which is very important for small programs.
Fast code execution
Accelerated the process of calling Python functions due to:
- reuse of allocated memory on the stack;
- reducing the number of memory allocations;
- minimization of memory consumed per function call (got rid of unnecessary debugging information).
Accelerated operation of recursive functions due to the removal of redundant intermediate calls within the interpreter.
Adaptive specialization is typical in the interpreter. Despite the fact that Python is a dynamically typed language, in many cases functions always work with values of the same types. This is called stability of types. If during code execution, the interpreter notices that type stability is present in some code fragment, then the interpreter will replace this code fragment with specialized code that works faster with specific types.
Examples of some typical constructions and performance gains in Python 3.11:
Code | Description | Acceleration |
---|---|---|
x+x; x*x; x-x; |
Sum, product and subtraction for standard types int, float, str. | 10% |
a[i] |
Retrieving value by index in list, tuple, dict. | 10-25% |
f(arg) |
Calling typical built-in C functions, such as len, str, immediately calls C code, bypassing the interpreter. | 20% |
Detailed description of the technology: PEP 659 – Specializing Adaptive Interpreter.
The most important thing is that you don't need to rewrite the code to take advantage of these advantages. It is enough to simply change the interpreter to version 3.11.
We remind you that the official release of Python 3.11 is scheduled for October 3, 2022.
You can download Python 3.11 before its official release from prerelease pages.
Source: What's New In Python 3.11.