pybind11-numpy-example

What

A simple example of how to use pybind11 with numpy.

This C++/Python library creates a std::vector of 16-bit ints, and provides a Python interface to the contents of this vector in a few different ways:

  • a Python List (copy the data)

  • a NumPy ndarray (copy the data).

  • a NumPy ndarray (move the data).

Why

Python Lists are great! However, when storing many small elements of the same type, a Numpy array is much faster and uses a lot less memory:

Memory used vs number of elements

Time used vs number of elements

How

The pybind11 code is in src/pybind11_numpy_example_python.cpp.

The python project is defined in pyproject.toml and uses scikit-build-core.

Each tagged commit triggers a GitHub action job which uses cibuildwheel to build and upload wheels to PyPI.

The scripts used to generate the above plots are in scripts.

This repo was quickly set up using the SSC C++ Project Cookiecutter.