Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Call the same Fibonacci function from Python through [CFFI](https://cffi.readthe

Install [uv](https://docs.astral.sh/uv/getting-started/installation/), 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](docs/index.md).
instructions are in the
[getting-started guide](https://tleonhardt.github.io/Python_Interface_Cpp/#install-the-toolchain).

```sh
uv sync --locked
Expand Down Expand Up @@ -37,17 +38,18 @@ samples of one million calls. Speedup is relative to pure Python on the same mac

| Implementation | Median ns/call | Speedup vs. Python |
| ---------------- | -------------: | -----------------: |
| Python | 219.0 | 1.00× |
| CFFI (ABI) | 107.5 | 2.04× |
| Cython (wrapper) | 21.4 | 10.21× |
| Cython (typed) | 21.2 | 10.35× |
| SWIG | 30.6 | 7.17× |
| pybind11 | 39.4 | 5.56× |
| nanobind | 23.6 | 9.28× |
| PyO3 | 25.9 | 8.44× |
| 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](docs/benchmark.md) for how the calls are timed and how to change the workload.
the [benchmark guide](https://tleonhardt.github.io/Python_Interface_Cpp/benchmark/) for how the
calls are timed and how to change the workload.

```sh
uv run --locked python -m comparison.benchmark --markdown -n 20 --calls 1000000 --repeat 9
Expand All @@ -59,9 +61,10 @@ uv run --locked python -m comparison.benchmark --markdown -n 20 --calls 1000000
declarations and the native functions they call.
- [Rust extension](rust/) uses PyO3 and maturin. It exposes the same Python function as the C and
C++ examples.
- [The guide to the bindings](docs/bindings.md) explains when each approach fits.
[Workshop exercises](docs/exercises.md) cover typed loops, buffers, vectors, callbacks, and
passing Python sequences to Rust with PyO3.
- [The guide to the bindings](https://tleonhardt.github.io/Python_Interface_Cpp/bindings/) explains
when each approach fits.
[Workshop exercises](https://tleonhardt.github.io/Python_Interface_Cpp/exercises/) cover typed
loops, buffers, vectors, callbacks, and passing Python sequences to Rust with PyO3.

The original [slides](Interfacing_C_C++_with_Python.pdf) and
[lab handout](Interfacing_C_C++_with_Python-Exercises.pdf) accompany the
Expand Down
2 changes: 1 addition & 1 deletion comparison/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def main() -> None:
print()
print("| Implementation | Median ns/call | Speedup vs. Python |")
print("| --- | ---: | ---: |")
row = "| {name} | {ns:.1f} | {speedup:.2f}× |"
row = "| {name} | {ns:.1f} | {speedup:.1f}× |"
else:
print(f"{'Implementation':<18} {'ns/call':>10} {'speedup':>9}")
row = "{name:<18} {ns:>10.1f} {speedup:>8.2f}x"
Expand Down
Loading