Example of a rust implementation of certain python constructs.
The aims are twofold:
- replicate the functionality of the C++/pybind11 equivalent here, and compare the ease of implementation
- compare performance by implementing the some basic prime number generation and factorisation algorithms in python, C++ and rust.
pip install -e .[dev]pytestInstall pybind11-examples in the same env.
perf-testto run performance tests.
maturin build --releaseCompared to pybind11-examples. When I say easier I mean easier in the end ;)
- generators. Ever so slightly easier than pybind11 thanks to
PyErr::from_type. -
__init_subclass__. Was actually easier than in C++/pybind11, which requires a workaround for classmethods. - decorators. Definitely more tricky, but got there in the end thanks to SO.
- context managers. More boilerplate needed - in C++ I can just selectively expose template specialisations to python.
- constants
- enums
- vectorised functions
- type annotations (manually)
rust initially seemed faster but after some performance improvements of the algorithms (in all languages), overall speed is much improved and C++ has more than caught up. Here's the latest results, times are in milliseconds:
Updated results:
| function | parameters | rust | python | C++ |
|---|---|---|---|---|
| nth_prime | (100000,) | 8 | 93 | 5 |
| nth_prime | (1000000,) | 87 | 1031 | 87 |
| prime_factors | (2199023255551,) | 3 | 709 | 6 |
| prime_factors | (10000000000000068,) | 769 | nan | 578 |
| PrimeRange | (1000000000000000, 1000000000001000) | 322 | nan | 290 |
| is_prime | (10000000000000061,) | 764 | nan | 588 |
rustc 1.76, py03 0.20.3 g++ 11.4, pybind11 2.11.1 cpython 3.12