'Is passing numpy arrays to C++ via pybind supposed to be this slow?

I'm trying to write a C++ extension for image processing. My images are quite large: 2064x1543 for example. I found that it takes a long time to just pass this via the binding. Here's a minimal reproducible example:

I have a C++ file called example.cpp.:

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

void doNothing(std::vector<std::vector<std::array<double, 3>>> arr) {}

PYBIND11_MODULE(example, m) { m.def("doNothing", &doNothing);}

From which I export a Python extension with

g++ -Wall -fPIC -shared example.cpp $(python3 -m pybind11 --includes) -O3 -o example$(python3-config --extension-suffix)

I have a test.py to time it:

from time import time
import numpy as np
from example import doNothing

arr = np.random.uniform(size=(1543, 2064, 3))

start = time()
doNothing(arr)
print(time() - start)

Which comes out to around 0.5 s on my machine. Seems like a long time! Am I doing something wrong? Can this be sped up?

My context: I've been using Python for years but totally new to C++



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source