| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 01d1a07 commit 0587329
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,6 @@ | |||
| 1 | + from xtensor_python_benchmark import pybind_sum_array | ||
| 2 | + import numpy as np | ||
| 3 | + | ||
| 4 | + u = np.ones(1000000, dtype=float) | ||
| 5 | + from timeit import timeit | ||
| 6 | + print (timeit ('pybind_sum_array(u)', setup='from __main__ import u, pybind_sum_array', number=1000)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | #include "pybind11/pybind11.h" | |
| 2 | + #include "pybind11/numpy.h" | ||
| 2 | 3 | #include "xtensor/xtensor.hpp" | |
| 3 | 4 | #include "xtensor/xarray.hpp" | |
| 4 | 5 | #include "xtensor-python/pyarray.hpp" | |
@@ -19,5 +20,15 @@ PYBIND11_PLUGIN(xtensor_python_benchmark) | |||
| 19 | 20 | } | |
| 20 | 21 | ); | |
| 21 | 22 | ||
| 23 | + m.def("pybind_sum_array", [](py::array_t<double> const& x) { | ||
| 24 | + double sum = 0; | ||
| 25 | + size_t size = x.size(); | ||
| 26 | + const double* data = x.data(0); | ||
| 27 | + for(size_t i = 0; i < size; ++i) | ||
| 28 | + sum += data[i]; | ||
| 29 | + return sum; | ||
| 30 | + } | ||
| 31 | + ); | ||
| 32 | + | ||
| 22 | 33 | return m.ptr(); | |
| 23 | 34 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments