Cython
CPython: a C implementation of Python. (others include PyPy)
Cython: C-extension of Python, allows to compile python code for speeding up.
The extension of Cython file is .pyx
Example
primes.pyx
:
def primes(int nb_primes):
cdef int n, i, len_p
cdef int p[1000]
if nb_primes > 1000:
nb_primes = 1000
len_p = 0 # The current number of elements in p.
n = 2
while len_p < nb_primes:
# Is n prime?
for i in p[:len_p]:
if n % i == 0:
break
# If no break occurred in the loop, we have a prime.
else:
p[len_p] = n
len_p += 1
n += 1
# Let's return the result in a python list:
result_as_list = [prime for prime in p[:len_p]]
return result_as_list
to compile it:
setup.py
from setuptools import setup
from Cython.Build import cythonize
setup(
ext_modules=cythonize("primes.pyx"),
)
python setup.py build_ext --inplace
to use it, in another python file:
import primes
primes.primes(10)
C library
from libc.stdlib cimport atoi
cdef parse_charptr_to_py_int(char* s):
assert s is not NULL, "byte string value is NULL"
return atoi(s) # note: atoi() has no error detection!
from libc.math cimport sin
cdef double f(double x):
return sin(x * x)
C++ library
# distutils: language=c++
from libcpp.vector cimport vector
def primes(unsigned int nb_primes):
cdef int n, i
cdef vector[int] p
p.reserve(nb_primes) # allocate memory for 'nb_primes' elements.
n = 2
while p.size() < nb_primes: # size() for vectors is similar to len()
for i in p:
if n % i == 0:
break
else:
p.push_back(n) # push_back is similar to append()
n += 1
# Vectors are automatically converted to Python
# lists when converted to Python objects.
return p