Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only T&Cs apply

Find out more

Where to add types?

In this video Dr. Jussi Enkovaara shows how Cython's annotate tool can help in finding places where typing information is useful.
11
In complex programs it is not always straight forward to see whether you have introduced static typing in the all the relevant parts of your Cython module. However, Cython provides you easy-to-use tool for investigating that. With the Cython annotate tool you can start the Cython with -a followed by the name of your Cython module. What Cython does here, it creates a report about the cythonization process storing it in filename but now with the HTML-form. You can now launch this file with your favourite browser,
57.6
after which you can see in yellow all the parts of the program that involve some Python overhead, and with white those parts that translated into the pure C. In this particular case we can see now that for our kernel function here, it is practically all C, only when you return from the function there is some Python overhead involved there. However, if you forget to introduce static typing in some parts of the program, we can look how the report would look like. So let’s edit our module a bit, and just remove the static type defintion from the count argument here. Let’s save the file and now create the report again with the annotate tool.
118.1
I can launch now this report into a new tab, and if we compare to the previous one here, you can see that now there is in the actual while loop some Python overhead involded, and you can also the see actual code, C code, that this translates, so we can see some Python API calls here, and you can see also that while incrementing the count there is some Python overhead because count no longer is a pure C integer, but a Python integer object.
155.7
The Cython annotate tool provides easy way to figure out whether you have introduced static typing in all the relevant parts of the program.

In complex programs it is not always straight forward to see whether you have introduced static typing in the all the relevant parts of your Cython module.

Cython provides an easy-to-use annotate tool, which allows one to investigate the translation to C with web browser.

This article is from the free online

Python in High Performance Computing

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now