Cython tutorial: How to speed up Python

How to use Cython and its Python-to-C compiler to give your Python applications a rocket boost

1 2 Page 2
Page 2 of 2

Also note that the division operation in line 8 is also highlighted; this is because Cython automatically inserts tests for division by zero, and raises a Python exception if that’s the case. You can disable this with the cdivision compiler directive, or by way of a decorator on the function (@cython.cdivision(True)).

Cython resources

Now that you have some idea of how Cython integrates with an existing Python app, the next step is to put it to work in your own applications, and to make use of other resources out there:

  • The official Cython documentation is your best next step. It contains many tutorials that cover separate aspects of Cython, such as working with Python’s native array datatype.
  • The Cython code repository on GitHub also has a wiki with useful tips.
  • If you have a question that isn’t covered by the docs, ask in the official cython-users mailing list. Cython’s core developers read the list and provide highly technical, detailed answers.
  • You’ll find a treasure trove of Cython questions and answers on Stack Overflow. If you have a specific technical question about using Cython, odds are someone has answered it.
  • Cython by Kurt Smith is a book-length exploration of working with Cython that covers various features of Cython and details many usage scenarios. Note that the book only refers to the feature sets for Cython up to version 0.21, although most of the material remains valid and applicable.
  • Another useful book is High Performance Python by Micha Gorelick. It isn’t as comprehensive as Smith’s book, but offers a broader perspective on Cython as one among several technologies that can be used to improve the performance of Python applications.

Copyright © 2018 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2