Using Cython with Jupyter
Optimizing Cython code requires substantial trial and error. Fortunately, Cython tools can be conveniently accessed through the Jupyter notebook for a more streamlined and integrated experience.
You can launch a notebook session by typing jupyter notebook
in the command line and you can load the Cython magic by typing %load_ext cython
in a cell.
As already mentioned earlier, the %%cython
magic can be used to compile and load the Cython code inside the current session. As an example, we may copy the contents of cheb.py
into a notebook cell:
%%cython import numpy as np cdef int max(int a, int b): return a if a > b else b cdef int chebyshev(int x1, int y1, int x2, int y2): return max(abs(x1 - x2), abs(y1 - y2)) def c_benchmark(): a = np.random.rand(1000, 2) b = np.random.rand(1000, 2) for x1, y1 in a: for x2, y2 in b: chebyshev(x1, x2, y1, y2)
A useful feature of the %%cython
magic...