The spiritual little brother of
less_slow.cpp
. Assuming Python is used in a different setting than C++, this repository focuses more on scripting, tool integration, and data processing.
Much of modern code suffers from common pitfalls: bugs, security vulnerabilities, and performance bottlenecks. University curricula often teach outdated concepts, while bootcamps oversimplify crucial software development principles.
This repository offers practical examples of writing efficient Python code. The topics range from basic micro-kernels executing in a few nanoseconds to more complex constructs involving parallel algorithms, coroutines, and polymorphism. Some of the highlights include:
- Using callbacks, lambdas, and
yield
-ing functions are much faster than iterator-based routines, unlike Rust and C++. - Not every all composite structures are equally fast:
namedtuple
is slower than {dataclass
,class
} is slower thandict
. - Error handling with status codes can be both 50% faster and 2x slower than exceptions, depending on your design.
- NumPy-based logic can be much slower than
math
functions depending on the shape of the input. - JIT compilers like Numba can make your code 2x slower, even if the kernels are precompiled if they are short.
To read, jump to the less_slow.py
source file and read the code snippets and comments.
If you are familiar with Python and want to review code and measurements as you read, you can clone the repository and execute the following commands.
pytest less_slow.py
If you have uv
installed, you can run the benchmark script with it. This also
allows running the script with different Python versions. Here is an example of
running the script with Python 3.12.
The --no-sync
flag is used to prevent uv
from creating an uv.lock file or
modifying an existing .venv
folder.
uv run --python="3.12" --no-sync \
--with "pytest" \
--with "pytest-benchmark" \
--with "numpy" \
--with "pandas" \
--with "pyarrow" \
pytest -ra -q less_slow.py