Call the same Fibonacci function from Python through CFFI, Cython, SWIG, pybind11, nanobind, and PyO3.
Install uv, a C/C++17 compiler, Rust with Cargo, and the zlib development headers. The default interpreter is CPython 3.15. Platform-specific instructions are in the getting-started guide.
uv sync --locked
uv run --locked python -c "from rust_fibonacci import compute_fibonacci; print(compute_fibonacci(20))"
uv run --locked python -m pytestThe Rust call prints 17711. The original workshop starts its sequence at 1, 2, 3, 5, so its
index 20 corresponds to the conventional Fibonacci number F(22).
| To | Run |
|---|---|
| Build the extensions and run tests | make test |
| Compare timings on your machine | make benchmark |
| Install the prek Git hook | make hooks |
| Run code quality checks | make check |
| Serve the Zensical guide | make docs |
| Build the site with strict validation | make docs-test |
On December 5, 2026, each implementation computed fib(20) = 17711 on an Apple M3 Max running macOS
27.2.1 and CPython 3.15.2 (Apple Clang 21.0.0, rustc 1.100.1). Each row shows the median of nine
samples of one million calls. Speedup is relative to pure Python on the same machine.
| Implementation | Median ns/call | Speedup vs. Python |
|---|---|---|
| Python | 219.0 | 1.0× |
| CFFI (ABI) | 107.5 | 2.0× |
| Cython (wrapper) | 21.4 | 10.2× |
| Cython (typed) | 21.2 | 10.4× |
| SWIG | 30.6 | 7.2× |
| pybind11 | 39.4 | 5.6× |
| nanobind | 23.6 | 9.3× |
| PyO3 | 25.9 | 8.4× |
To generate this table on your machine, run the following from the repository root after setup. See the benchmark guide for how the calls are timed and how to change the workload.
uv run --locked python -m comparison.benchmark --markdown -n 20 --calls 1000000 --repeat 9- C and C++ bindings share one scikit-build-core/CMake build. Compare the binding declarations and the native functions they call.
- Rust extension uses PyO3 and maturin. It exposes the same Python function as the C and C++ examples.
- The guide to the bindings explains when each approach fits. Workshop exercises cover typed loops, buffers, vectors, callbacks, and passing Python sequences to Rust with PyO3.
The original slides and lab handout accompany the 2019 workshop checkout. Its PyPy comparisons, Ruby extension, FastLZ exercises, and platform-specific build scripts are available there. For the current exercises, use the Zensical guide.