Python is a powerful programming language that is easy to learn and easy to work with, but it is not always the fastest to run—especially when you’re dealing with math or statistics. Third-party libraries like NumPy, which wrap C libraries, can improve the performance of some operations significantly, but sometimes you just need the raw speed and power of C directly in Python.
Cython was developed to make it easier to write C extensions for Python, and to allow existing Python code to be transformed into C. What’s more, Cython allows the optimized code to be shipped with a Python application without external dependencies.
In this tutorial we’ll walk through the steps needed to transform existing Python code into Cython, and to use it in a production application.
A Cython example
Let’s begin with a simple example taken from Cython’s documentation, a not-very-efficient implementation of an integral function:
Computers and Software Buyers Guide
Compare Computers and Laptops
Mobile Phones Buyers Guide
- Mobile Phones Buyers Guide
- Mobile Phones Accessories Buyers Guide
- All in one Printers Buyers Guide
- Fax Machines Buyers Guide
- Home Telephones Buyers Guide
Compare Mobile Phones
- Compare Mobile Phones
- Compare Mobile Phone Accessories
- Compare Smart Watches
- Compare All in One Printers
- Compare Fax Machines
- Compare Home Telephones
- Compare Home Telephone Accessories