pybind/pybind11

[QUESTION]: Speedup for converting numpy array to std::vector<T>

Open

#4.131 geöffnet am 10. Aug. 2022

Auf GitHub ansehen
 (2 Kommentare) (2 Reaktionen) (0 zugewiesene Personen)C++ (2.005 Forks)batch import
enhancementhelp wanted

Repository-Metriken

Stars
 (14.677 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 5T 2h) (10 gemergte PRs in 30 T)

Beschreibung

Required prerequisites

Problem description

I have a function foo which takes a std::vector as input, and I would like to expose the function to python, in which python programmers would call foo by passing in python lists or numpy arrays.

extern void foo(const std::vector<int64_t> &v);

PYBIND11_MODULE(test, m) {
    m.def("foo", &foo);
}

For numpy arrays, the performance of casting is a bit slower than I thought: in fact, it is even slower than converting a list to std::vector. After a bit of research, I found that to cast numpy arrays, one first converts numpy raw data to a PyObject (note for lists, this proc could be ignored) before converting it to T, and finally moves it to std::vector. For references, list_caster::load type_cast::load

So my question is, is this possible for a speed-up when casting a numpy array into std::vector? I have noticed that vector_buffer_impl does a pretty good job at converting a py::buffer to a std::vector, maybe this piece of code could be used when casting into std::vector. If this could be a way, I would volunteer to submit a pull request.

Any thoughts would be welcomed and appreciated. Thanks.

Contributor Guide