Skip to content

Commit 01d1a07

Browse files
authored
Merge pull request #28 from JohanMabille/benchmark
benchmarks
2 parents 3228270 + 6016e0c commit 01d1a07

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

benchmark/benchmark_pyarray.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 sum_array
2+
import numpy as np
3+
4+
u = np.ones(1000000, dtype=float)
5+
from timeit import timeit
6+
print (timeit ('sum_array(u)', setup='from __main__ import u, sum_array', number=1000))

benchmark/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "pybind11/pybind11.h"
2+
#include "xtensor/xtensor.hpp"
3+
#include "xtensor/xarray.hpp"
4+
#include "xtensor-python/pyarray.hpp"
5+
6+
#include <complex>
7+
8+
namespace py = pybind11;
9+
10+
PYBIND11_PLUGIN(xtensor_python_benchmark)
11+
{
12+
py::module m("xtensor_python_benchmark", "Benchmark module for xtensor python bindings");
13+
14+
m.def("sum_array", [](xt::pyarray<double> const& x) {
15+
double sum = 0;
16+
for(auto e : x)
17+
sum += e;
18+
return sum;
19+
}
20+
);
21+
22+
return m.ptr();
23+
}

benchmark/setup.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from setuptools import setup, Extension
2+
from setuptools.command.build_ext import build_ext
3+
import sys
4+
import os
5+
import setuptools
6+
7+
__version__ = '0.0.1'
8+
9+
10+
class get_pybind_include(object):
11+
"""Helper class to determine the pybind11 include path
12+
13+
The purpose of this class is to postpone importing pybind11
14+
until it is actually installed, so that the ``get_include()``
15+
method can be invoked. """
16+
17+
def __init__(self, user=False):
18+
self.user = user
19+
20+
def __str__(self):
21+
import pybind11
22+
return pybind11.get_include(self.user)
23+
24+
25+
ext_modules = [
26+
Extension(
27+
'xtensor_python_benchmark',
28+
['main.cpp'],
29+
include_dirs=[
30+
# Path to pybind11 headers
31+
get_pybind_include(),
32+
get_pybind_include(user=True),
33+
os.path.join(sys.prefix, 'include'),
34+
os.path.join(sys.prefix, 'Library', 'include')
35+
],
36+
language='c++'
37+
),
38+
]
39+
40+
41+
def has_flag(compiler, flagname):
42+
"""Return a boolean indicating whether a flag name is supported on
43+
the specified compiler.
44+
"""
45+
import tempfile
46+
with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f:
47+
f.write('int main (int argc, char **argv) { return 0; }')
48+
try:
49+
compiler.compile([f.name], extra_postargs=[flagname])
50+
except setuptools.distutils.errors.CompileError:
51+
return False
52+
return True
53+
54+
55+
def cpp_flag(compiler):
56+
"""Return the -std=c++14 compiler flag and errors when the flag is
57+
no available.
58+
"""
59+
if has_flag(compiler, '-std=c++14'):
60+
return '-std=c++14'
61+
else:
62+
raise RuntimeError('C++14 support is required by xtensor!')
63+
64+
65+
class BuildExt(build_ext):
66+
"""A custom build extension for adding compiler-specific options."""
67+
c_opts = {
68+
'msvc': ['/EHsc'],
69+
'unix': [],
70+
}
71+
72+
if sys.platform == 'darwin':
73+
c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']
74+
75+
def build_extensions(self):
76+
ct = self.compiler.compiler_type
77+
opts = self.c_opts.get(ct, [])
78+
if ct == 'unix':
79+
opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
80+
opts.append(cpp_flag(self.compiler))
81+
if has_flag(self.compiler, '-fvisibility=hidden'):
82+
opts.append('-fvisibility=hidden')
83+
elif ct == 'msvc':
84+
opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version())
85+
for ext in self.extensions:
86+
ext.extra_compile_args = opts
87+
build_ext.build_extensions(self)
88+
89+
setup(
90+
name='xtensor_python_benchmark',
91+
version=__version__,
92+
author='Sylvain Corlay',
93+
author_email='sylvain.corlay@gmail.com',
94+
url='https://github.com/pybind/python_example',
95+
description='An example project using xtensor-python',
96+
long_description='',
97+
ext_modules=ext_modules,
98+
install_requires=['pybind11==2.0.1'],
99+
cmdclass={'build_ext': BuildExt},
100+
zip_safe=False,
101+
)

test/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def build_extensions(self):
9595
description='An example project using xtensor-python',
9696
long_description='',
9797
ext_modules=ext_modules,
98-
install_requires=['pybind11==1.8.1'],
98+
install_requires=['pybind11==2.0.1'],
9999
cmdclass={'build_ext': BuildExt},
100100
zip_safe=False,
101101
)

0 commit comments

Comments
 (0)