Skip to content

Commit 9bd3076

Browse files
Add an example of the same wrapper using cython (mathbunnyru#2)
* Add an example of the same wrapper using cython * Update .gitignore * Update cython_sieve.pyx * Update sieve.pxd Co-authored-by: Ayaz Salikhov <[email protected]>
1 parent cdc20f5 commit 9bd3076

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,6 @@ dmypy.json
130130

131131
# macOS files
132132
.DS_Store
133+
134+
# Cython generated file
135+
cython_sieve.cpp

cython_sieve.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cimport sieve
2+
3+
4+
def sieve_of_eratosthenes(size_t n):
5+
'''
6+
Return list of prime numbers until given number. Implemented in Cython.
7+
'''
8+
return sieve.SieveOfEratosthenes(n)

setup.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from setuptools import setup, Extension
22
import sysconfig
33

4+
from Cython.Build import cythonize
5+
46

57
language = 'c++'
68
std = 'c++17'
@@ -18,9 +20,16 @@
1820
language=language
1921
)
2022

23+
cython_extension = Extension(
24+
'cython_sieve',
25+
sources=['cython_sieve.pyx', 'sieve.cpp'],
26+
extra_compile_args=extra_compile_args,
27+
language='c++',
28+
)
29+
2130
setup(
2231
name='cpp_python_extension',
2332
version='1.0',
2433
description='This is Example module written in C++',
25-
ext_modules=[extension]
34+
ext_modules=cythonize([extension, cython_extension]),
2635
)

sieve.pxd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from libcpp.vector cimport vector
2+
3+
4+
cdef extern from "sieve.h" namespace "abc":
5+
vector[size_t] SieveOfEratosthenes(size_t n)

test_benchmark.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import pytest
22
import cpp_python_extension
3+
import cython_sieve
34

45

56
@pytest.mark.parametrize("input", [1000, 100000, 1000000])
67
def test_cpp(benchmark, input):
78
benchmark(cpp_python_extension.sieve_of_eratosthenes, input)
9+
10+
@pytest.mark.parametrize("input", [1000, 100000, 1000000])
11+
def test_cyth(benchmark, input):
12+
benchmark(cython_sieve.sieve_of_eratosthenes, input)

0 commit comments

Comments
 (0)