Skip to content
This repository was archived by the owner on May 11, 2024. It is now read-only.

Commit 1be2915

Browse files
committed
Init commit
0 parents  commit 1be2915

20 files changed

+498
-0
lines changed

.gitignore

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
98+
__pypackages__/
99+
100+
# Celery stuff
101+
celerybeat-schedule
102+
celerybeat.pid
103+
104+
# SageMath parsed files
105+
*.sage.py
106+
107+
# Environments
108+
.env
109+
.venv
110+
env/
111+
venv/
112+
ENV/
113+
env.bak/
114+
venv.bak/
115+
116+
# Spyder project settings
117+
.spyderproject
118+
.spyproject
119+
120+
# Rope project settings
121+
.ropeproject
122+
123+
# mkdocs documentation
124+
/site
125+
126+
# mypy
127+
.mypy_cache/
128+
.dmypy.json
129+
dmypy.json
130+
131+
# Pyre type checker
132+
.pyre/
133+
134+
# pytype static type analyzer
135+
.pytype/
136+
137+
# Cython debug symbols
138+
cython_debug/
139+
140+
# PyCharm
141+
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
142+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
143+
# and can be added to the global gitignore or merged into this file. For a more nuclear
144+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
145+
#.idea/
146+
147+
*/_skbuild

.vscode/settings.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"files.associations": {
3+
"cctype": "cpp",
4+
"clocale": "cpp",
5+
"cmath": "cpp",
6+
"cstdarg": "cpp",
7+
"cstddef": "cpp",
8+
"cstdio": "cpp",
9+
"cstdlib": "cpp",
10+
"cstring": "cpp",
11+
"ctime": "cpp",
12+
"cwchar": "cpp",
13+
"cwctype": "cpp",
14+
"array": "cpp",
15+
"atomic": "cpp",
16+
"strstream": "cpp",
17+
"bit": "cpp",
18+
"*.tcc": "cpp",
19+
"chrono": "cpp",
20+
"cstdint": "cpp",
21+
"deque": "cpp",
22+
"forward_list": "cpp",
23+
"map": "cpp",
24+
"unordered_map": "cpp",
25+
"unordered_set": "cpp",
26+
"vector": "cpp",
27+
"exception": "cpp",
28+
"algorithm": "cpp",
29+
"functional": "cpp",
30+
"iterator": "cpp",
31+
"memory": "cpp",
32+
"memory_resource": "cpp",
33+
"numeric": "cpp",
34+
"optional": "cpp",
35+
"random": "cpp",
36+
"ratio": "cpp",
37+
"string": "cpp",
38+
"string_view": "cpp",
39+
"system_error": "cpp",
40+
"tuple": "cpp",
41+
"type_traits": "cpp",
42+
"utility": "cpp",
43+
"fstream": "cpp",
44+
"initializer_list": "cpp",
45+
"iosfwd": "cpp",
46+
"iostream": "cpp",
47+
"istream": "cpp",
48+
"limits": "cpp",
49+
"new": "cpp",
50+
"ostream": "cpp",
51+
"sstream": "cpp",
52+
"stdexcept": "cpp",
53+
"streambuf": "cpp",
54+
"thread": "cpp",
55+
"cinttypes": "cpp",
56+
"typeindex": "cpp",
57+
"typeinfo": "cpp"
58+
}
59+
}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Python C++ bindings benchmark
2+
3+
This repo contains benchmark for the performance of the C++ bindings including Cython, Pybind11, etc. The build process is handled by scikit-build.
4+
5+
To build and run the benchmark:
6+
```sh
7+
python build.py
8+
python test.py
9+
```

build.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import shutil, subprocess
2+
from pathlib import Path
3+
4+
root = Path(__file__).resolve().parent
5+
6+
subprocess.check_call(['python', 'setup.py', 'build_ext', '--inplace'], cwd="cython-base")
7+
subprocess.check_call(['python', 'setup.py', 'build_ext', '--inplace'], cwd="cython-derived")
8+
subprocess.check_call(['python', 'setup.py', 'build_ext', '--inplace'], cwd="pybind11-base")
9+
subprocess.check_call(['python', 'setup.py', 'build_ext', '--inplace'], cwd="pybind11-derived")
10+
11+
for f in root.glob("*/_skbuild/*/cmake-install/*"):
12+
shutil.copy2(f, root / f.name)
13+
14+
print(">> build finished.")

cython-base/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
project(cython_base)
4+
5+
find_package(PythonExtensions REQUIRED)
6+
find_package(Cython REQUIRED)
7+
find_package(NumPy REQUIRED)
8+
include_directories(${NumPy_INCLUDE_DIRS})
9+
10+
get_filename_component(main_dir ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
11+
include_directories(${main_dir})
12+
13+
add_cython_target(cython_base CXX)
14+
add_library(cython_base MODULE ${cython_base})
15+
python_extension_module(cython_base)
16+
install(TARGETS cython_base LIBRARY DESTINATION .)

cython-base/cython_base.pxd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from libcpp.vector cimport vector
2+
3+
cdef extern from "impl.hpp":
4+
cdef cppclass SortedVector:
5+
SortedVector(const vector[float] &)
6+
cdef float csum "sum" (const SortedVector&)
7+
8+
cdef class CySortedVector: # wrapped sorted vector
9+
cdef SortedVector *_ptr

cython-base/cython_base.pyx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from cython.operator cimport dereference as deref
2+
3+
cdef class CySortedVector:
4+
def __cinit__(self, data=None):
5+
cdef vector[float] cdata
6+
7+
if type(self) is CySortedVector:
8+
if data is not None:
9+
cdata = data
10+
self._ptr = new SortedVector(cdata)
11+
12+
def __dealloc__(self):
13+
if type(self) is CySortedVector:
14+
del self._ptr
15+
16+
def sum(CySortedVector vec):
17+
return csum(deref(vec._ptr))

cython-base/setup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from skbuild import setup
2+
3+
setup(
4+
name="cython-base",
5+
version="0.0.1",
6+
packages=['cython_base'],
7+
)

cython-derived/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
project(cython_derived)
4+
5+
find_package(PythonExtensions REQUIRED)
6+
find_package(Cython REQUIRED)
7+
find_package(NumPy REQUIRED)
8+
include_directories(${NumPy_INCLUDE_DIRS})
9+
10+
get_filename_component(main_dir ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
11+
include_directories(${main_dir})
12+
include_directories(${main_dir}/cython-base) # for cython header
13+
14+
add_cython_target(cython_derived CXX)
15+
add_library(cython_derived MODULE ${cython_derived})
16+
python_extension_module(cython_derived)
17+
install(TARGETS cython_derived LIBRARY DESTINATION .)

cython-derived/cython_derived.pxd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from libcpp.vector cimport vector
2+
from cython_base cimport SortedVector, CySortedVector
3+
4+
cdef extern from "impl.hpp":
5+
cdef cppclass AccumulatedVector(SortedVector):
6+
AccumulatedVector(const vector[float] &)
7+
cdef float csum "sum" (const AccumulatedVector&)
8+
9+
cdef class CyAccumulatedVector(CySortedVector):
10+
pass

0 commit comments

Comments
 (0)