Skip to content

krisfur/python-bindings

Repository files navigation

Python bindings examples

Simple repo serving as a basic template showing how to quickly bind other programming languages to python.

Overview

This project demonstrates parallel square root computation using three different approaches:

  • Python: Using multiprocessing
  • C++: Using pybind11 with std::async
  • Rust: Using PyO3 with rayon

Sample outcome on my local device:

=== Python (multiprocessing) ===
Time: 237.20ms

=== C++ (pybind11) ===
Time: 0.46ms

=== Rust (PyO3) ===
Time: 0.61ms

Prerequisites

  • Python 3.8+
  • uv (Python package manager)
  • C++ compiler (for C++ bindings)
  • Rust and Cargo (for Rust bindings)

Setup

1. Install dependencies

uv sync

2. Build C++ bindings (pybind11)

uv run python setup.py build_ext --inplace

This will create cpp_bindings.cp3xx-*.pyd in the project root.

3. Build Rust bindings (PyO3)

uv run maturin develop --release

Running

uv run python main.py

This will compare the performance of all three implementations.

Project Structure

python-bindings/
├── main.py              # Main benchmark script
├── cpp_bindings.cpp     # C++ implementation
├── rust_src/
│   └── lib.rs          # Rust implementation
├── setup.py            # C++ build configuration
├── Cargo.toml          # Rust build configuration
└── pyproject.toml      # Python project configuration

Technologies

C++ (pybind11)

  • Modern C++ bindings for Python
  • Uses std::async for parallel execution
  • Thread pool based on hardware concurrency

Rust (PyO3)

  • Safe Rust bindings for Python
  • Uses rayon for data parallelism
  • Compiled with release optimizations

⚠️ This project uses uv for package management as seen in pyproject.toml. The C++ and Rust implementations are compiled extensions that must be built before use. The build artifacts (.pyd files on Windows, .so files on Unix) are gitignored and must be rebuilt after cloning.

About

Simple example of how to bind C++ and Rust code to python.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published