Starting with thetime command
time is only available on UNIX-based systems, so if you’re working with
Windows, you can skip this first step.time command
gives you a bird’s-eye view of your script’s performance, measuring everything
from CPU usage to memory consumption.
Let’s start with a practical example. Create a script that demonstrates
different algorithmic approaches:
bench.py
time command in action:
terminal
terminal
time command reveals three crucial metrics:
- Real time (
realortotal): Wall-clock time from start to finish - User time (
user): CPU time spent in user mode (your Python code executing—loops, calculations, memory operations) - System time (
sysorsystem): CPU time spent in kernel mode (system calls, file I/O, memory allocation from the OS)
Precision Benchmarking with hyperfine
While time gives you the basics, hyperfine transforms benchmarking into a
science. It runs multiple iterations, provides statistical analysis, and even
generates beautiful comparison charts.
After having
installed hyperfine,
you can get started pretty quickly:
terminal
time run
can’t provide.
You can also compare commands with hyperfine:
terminal
Function-Level Precision with timeit
When you need to focus on specific functions rather than entire scripts,
Python’s built-in timeit module becomes your microscope. It’s designed to
minimize timing overhead and provide accurate measurements of small code
snippets.
Here is an example measuring the functions we previously created:
time.py
terminal
timeit lies in its surgical precision. While our previous tools
measured entire script execution, timeit isolates the exact performance
characteristics of individual functions. This granular approach becomes
invaluable when you’re optimizing specific bottlenecks rather than entire
applications.
Create Benchmarks from Existing Test Suites
First, let’s create proper tests for our sorting functions. The first step is to install the testing library:pytest:
terminal
test_sort.py
terminal
Turning the test cases into benchmarks
Now comes the real magic. Installpytest-codspeed and transform these
correctness tests into performance benchmarks with minimal changes:
terminal
benchmark fixture as a parameter and using it to
wrap the execution of the sort algorithm:
test_sort.py
terminal
pytest-codspeed automatically determined the optimal number of
iterations: 6 runs for the slow bubble sort versus 1,005 runs for the
lightning-fast quicksort. This intelligent adaptation ensures statistical
significance regardless of your algorithm’s performance characteristics.
Learn more about the plugin in
the pytest-codspeed reference.
The Foundation for Continuous Performance Testing
What makes this approach transformative isn’t just the numbers—it’s how easily it integrates into your existing workflow. You’ve just created the foundation for a performance monitoring system that can run locally during development and automatically in CI/CD pipelines. This is the first step toward performance-conscious development. While you can now validate performance locally, the real power emerges when you integrate these benchmarks into your continuous integration pipeline. Every pull request becomes a performance checkpoint, every deployment includes performance validation, and performance regressions are caught before they reach production. The CodSpeed ecosystem makes this transition seamless—from local development to continuous testing in just a few configuration steps. Check out this guide:Choosing Your Benchmarking Strategy
Each tool serves a specific purpose in your performance toolkit:- Use the
timecommand when you need a quick sanity check of overall script performance or want to understand system resource usage. It’s perfect for comparing different implementations at the application level. - Choose
hyperfinewhen you need statistical rigor for command-line tools or want to track performance across different input parameters. Its warmup runs and statistical analysis make it ideal for detecting small performance changes. - Reach for
timeitwhen you’re optimizing specific functions or comparing different algorithmic approaches. Its focus on eliminating timing overhead makes it perfect for micro-benchmarks. - Implement
pytest-codspeedwhen performance becomes a first-class concern in your development process. It transforms performance testing from an afterthought into an integral part of your test suite.
Suggested Reading
pytest-codspeed documentation
A more advanced resource on writing benchmarks with pytest-codspeed.
Running Python Benchmarks in your CI
A more advanced resource on continuous performance testing in Python