Profiling 101 for Python Developers: What is a Profiler? 1/6

By Sümer Cip, on Jan 30, 2020

Blog post series index:

  1. What is a Profiler? (you are here)
  2. The Many Types of Profilers
  3. List of Existing Python Profilers
  4. Picking the Right Profiler
  5. Profiles visualizations
  6. Using Blackfire

Back to the basics

Let’s start with the basics: What is a profiler? In computing, a profiler is a software (sometimes hardware) that makes measurements of the runtime performance of some code. Such tools are designed to give the metrics you need to find the slowest parts of the code, so that you can optimize what really matters.

As it happens at runtime, profiling is a form of dynamic code analysis: it measures the code as it runs. It enables to show how the code behaves, where static analysis can only interpret stand-alone code blocks, with no interaction with other services, such as a DB, the network and more.

Profilers can gather a wide variety of metrics: wall time, cpu time, network or memory consumption, I/O operations, etc.

Why do we need profilers?

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

This famous observation by Donald Knuth made in 1960s is unfortunately often reduced to: “premature optimization is the root of all evil”. This would leave the critical 3% out, where the idea is to balance the time you gain thanks to the optimizations versus the time you spend delivering these optimizations.

Now here’s another famous phrase: “you cannot optimize what you cannot measure”. That’s precisely where a profiler does help: find what are the 3%.

“Lean” performance measurements in Python

In the Python world, there are many quick and dirty ways to get a time value for the execution of some code, like with time() and print() statements:

t0=time.time()
// do something
print("elapsed=%d" % (time.time() - t0))

But that doesn’t tell you anything about how the code has been executed. Time is just a consequence of what happened in the code. Getting only a time value will not lead you anywhere.

Tooling up with Blackfire

A profiler:

  • tells you which part of your code shows critical optimization opportunities,
  • tells you how your code behaves and results in those critical opportunities,
  • when used during development, can help you prevent issues from ever happening,
  • when used in production, gives you all of the context that you need to build faster applications for real use cases.

Blackfire can do all of that, with blazing fast installation and no overhead for end-users.

Stay with us for the next article of the series and learn why.

Blackfire Profiler for Python public beta is now open! Get started in minutes.

Next article: The Many Types of Profilers

Sümer Cip

Sümer Cip is currently a Senior Software Engineer at Blackfire. He has been coding for nearly 20 years and contributed to many open source projects in his career. He has a deep love for Python and obsession with code performance and low-level stuff.