Skip to content

Commit 0587329

Browse files
committed
pybind array_t benchmark added
1 parent 01d1a07 commit 0587329

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

benchmark/benchmark_pyarray2.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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))

benchmark/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "pybind11/pybind11.h"
2+
#include "pybind11/numpy.h"
23
#include "xtensor/xtensor.hpp"
34
#include "xtensor/xarray.hpp"
45
#include "xtensor-python/pyarray.hpp"
@@ -19,5 +20,15 @@ PYBIND11_PLUGIN(xtensor_python_benchmark)
1920
}
2021
);
2122

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+
2233
return m.ptr();
2334
}

0 commit comments

Comments
 (0)