Skip to content

Learning how to write "Less Slow" code in Python, from numerical micro-kernels to coroutines, ranges, and polymorphic state machines

License

Notifications You must be signed in to change notification settings

ashvardanian/less_slow.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Less Slow Python

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.

Less Slow Python

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 than dict.
  • 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.

Reproducing the Benchmarks

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

Running benchmarks with uv

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

About

Learning how to write "Less Slow" code in Python, from numerical micro-kernels to coroutines, ranges, and polymorphic state machines

Topics

Resources

License

Stars

Watchers

Forks

Languages