Book Image

Mastering Python High Performance

Book Image

Mastering Python High Performance

Overview of this book

Table of Contents (15 chapters)

Getting to know our new best friends: the profilers


After all the theory and generic examples from the previous chapter, it is time for some real Python. So, let's begin with two of the most known and used Python profilers: cProfile and line_profiler. They will help us profile our code in two different ways.

On one hand, we have cProfile (https://docs.python.org/2/library/profile.html#module-cProfile), It comes by default with Python since version 2.5 and is the recommended profiler for most use cases. At least that is what the official Python documentation says about it. On the other hand, we have line_profiler (https://github.com/rkern/line_profiler), which is not an official part of the Python programming language, but it's a well-known profiler out there.

Let's go over both of them in more detail.

cProfile

Like I've already mentioned, cProfile comes by default with the standard Python interpreter (cPython) since version 2.5. Other versions, such as PyPy, don't have it. It is a deterministic...