Chapter 8. Speeding Up NumPy with Cython

Python combined with the NumPy library provides the user with a tool to write highly complex functions and analysis. As the size and complexity of code grow, the number of inefficiencies in the code base starts to creep in. Once the project is in its completion stages, developers should start focusing on the performance of the code and analyze the bottlenecks. Python provides many tools and libraries to create optimized and faster-performing code.

In this chapter, we will be looking at one such tool called Cython. Cython is a static compiler for Python and the language "Cython," which is particularly popular among developers working on scientific libraries/numerical computing. Many famous analytics libraries written in Python make intensive use of Cython (pandas, SciPy, scikit-learn, and so on).

The Cython programming language is a superset of Python and the user still enjoys all the functionalities and higher level constructs provided by Python the language. In this chapter, we will look into a number of reasons why Cython works and you will learn how to convert Python code to Cython. This chapter is not a complete guide to Cython, however.

In this chapter, we will cover the following topics:

  • Installing Cython on our computer
  • Rewriting a small amount of Python code to the Cython version and analyzing it
  • Learning to use NumPy with Cython

The first step toward optimizing code

The questions that every developer should have in mind while optimizing their code are as follows:

  • What number of function calls is your code making?
  • Are there redundant calls?
  • How much memory is the code using?
  • Are there memory leaks?
  • Where are the bottlenecks?

The first four questions are mostly answered by profiler tools. You are advised to learn at least about one profiling tool. Profiling tools will not be covered in this chapter. In most cases, it is suggested to first try to optimize function calls and memory usage before diving into lower-level approaches such as Cython or assembly languages (in C-derived languages).

Once the bottlenecks have been identified and all the issues with algorithms and logic have been tackled, a Python developer can dive into the world of Cython to get extra speed out of your application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.47.25