How to accelerate array_t construction in pybind11
I used C++ to call python with Pytorch.
C++ generate a vector and send to Python for neural network to inference.
But send the vector is a time consuming process.
A vector contain 500000 float consume 0.5 second turning to array_t.
Is there a faster way to transfer vector to array_t? Any help will be appreciate!
Here is the part of code:
int main(){
        float list[500000];
        std::vector&lt;float&gt; v(list, list+length);
        py::array_t&lt;float&gt; args = py::cast(v); //consume 0.5 second
        py::module_ nd_to_tensor = py::module_::import(&quot;inference&quot;);
        py::object result = nd_to_tensor.attr(&quot;inference&quot;)(args);
}

I also tried the second way as below, but it take 1.4 second in Python to make vector into tensor:
PYBIND11_MAKE_OPAQUE(std::vector&lt;float&gt;);
PYBIND11_EMBEDDED_MODULE(vectorbind, m) {
    m.doc() = &quot;C++ type bindings created by py11bind&quot;;
    py::bind_vector&lt;std::vector&lt;float&gt;&gt;(m, &quot;Vector&quot;);
}

int main(){
    std::vector&lt;float&gt; v(list, list+length);
    py::module_ nd_to_tensor = py::module_::import(&quot;inference&quot;);
    py::object result = nd_to_tensor.attr(&quot;inference&quot;)(&amp;v);
}

Here is Python code:
def inference():
    tensor = torch.Tensor(Vector)


Problem solved
py::array_t&lt;float&gt; args = py::array_t&lt;float&gt;({length}, {4}, &amp;list[0]);

Directly init the array_t will be the best way
