Skip to main content

An example of using numpy with pybind11

Project description

pybind11-numpy-example

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

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 python/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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release. See tutorial on generating distribution archives.

Built Distributions

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page