Profiling 101 for Python Developers: What is a Profiler? 1/6
This article is the first of a series of six on Profilers in the Python world, and how Blackfire is the best-of-breed tool to introspect code behavior and optimize its performance.
Blog post series index:
- What is a Profiler? (you are here)
- The Many Types of Profilers
- List of Existing Python Profilers
- Picking the Right Profiler
- Profiles visualizations
- 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.
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
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
- 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