Basic functions and operations using Cython and Python.
Installation command for anaconda and pip:
$ conda install -c anaconda cythonor
$ pip install Cythonimport cython
A Cython source file has the name of the module followed by the extension .pyx. For example, given the source file examples_cy.pyx with a simple function which returns a string.
def hello_cython():
return 'Hello, Cython!'
The following step consist of creating the setup.py, which will be responsible for the compilation process.
from setuptools import setup
from Cython.Build import cythonize
setup(
name="Examples Cython",
ext_modules=cythonize("examples_cy.pyx")
)
Given that, the compilation step is done by running the command:
$ python setup.py build_ext --inplacefrom examples_cy import hello_cython
print(hello_cython())
Hello, Cython!
The following example, we will try to approximate the value $\large\pi$ with the idea of $\\tan^{-1}1=\frac{\pi}{4}$ using the power series of arctan, defined by:
$$\large 4 \sum_{n=0}^{N}\frac{(-1)^n}{2n+1} \approx \pi $$where $N$ tends to the infinity.
def pi_py(N):
pi = 0
for n in range(N):
pi += (-1)**n/(2*n + 1)
return 4*pi
print(pi_py(1000000))
3.1415916535897743
In the same Cython source file examples_cy.pyx, lets include the function and adapt it to be compiled.
cdef double pi_cy(int N):
cdef double pi = 0
cdef int n
for n in range(N):
pi += (-1)**n/(2*n + 1)
return 4*pi
p.s.: compile it again running the command:
$ python setup.py build_ext --inplacefrom examples_cy import pi_cy
# Time measurement over the situations
print('[Python] pi_py |', end=' ')
%timeit -n 5 -r 5 pi_py(1000000)
print('[Cython] pi_cy |', end=' ')
%timeit -n 5 -r 5 pi_cy(1000000)
[Python] pi_py | 429 ms ± 2.16 ms per loop (mean ± std. dev. of 5 runs, 5 loops each) [Cython] pi_cy | 21.1 ms ± 229 µs per loop (mean ± std. dev. of 5 runs, 5 loops each)
To enable support for Cython compilation in Jupyter Notebooks, we have to run firstly the command:
%load_ext Cython
It will allow the C functions declaration inside cells, using the magic function %%cython for multiple lines.
p.s.: the function call must be within the same cell
%%cython
cdef int factorial(int x):
if x <= 1:
return 1
return x*factorial(x - 1)
print(factorial(10))
3628800