Skip main navigation

£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). 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.
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,
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.
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.
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

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education