Profiling Cython
Cython provides a feature, called annotated view, that helps find which lines are executed in the Python interpreter and which are good candidates for ulterior optimizations. We can turn this feature on by compiling a Cython file with the -a
option. In this way, Cython will generate an HTML file containing our code annotated with some useful information. The usage of the -a
option is as follows:
$ cython -a cevolve.pyx $ firefox cevolve.html
The HTML file displayed in the following screenshot shows our Cython file line by line:
Each line in the source code can appear in different shades of yellow. A more intense color corresponds to more interpreter-related calls, while white lines are translated to regular C code. Since interpreter calls substantially slow down execution, the objective is to make the function body as white as possible. By clicking on any of the lines, we can inspect the code generated by the Cython compiler. For example, the v_y = x/norm
line checks that the...