Skip to content

Example of using pybind11 with numpy and publishing to PyPI and conda-forge

License

Notifications You must be signed in to change notification settings

ssciwr/pybind11-numpy-example

Repository files navigation

pybind11-numpy-example

License: MIT PyPI Release Conda Release Python Versions GitHub Workflow Status Documentation Status

What

A simple example of how to use pybind11 with numpy and publish this as a library on PyPI and conda-forge.

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 package 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 a new release including binary wheels for all platforms to PyPI.

The conda-forge package is generated from this recipe, and automatically updates when a new version is uploaded 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.