Using SimpleBench๐
The simplebench module provides a benchmarking framework to measure the performance of Python code. It is designed to be easy to use while providing powerful customization and extension capabilities.
In the simplest case, you can create a benchmark by decorating a function with @simplebench.benchmark and adding a call to simplebench.main() to your script. This instantly gives you a full-featured benchmark suite that can be run from the command line.
This is all the code you need to write to create a simple benchmark:
1import simplebench
2
3@simplebench.benchmark
4def addition_benchmark():
5 """A simple addition benchmark of Python's built-in sum function."""
6 sum(range(1000))
7
8if __name__ == "__main__":
9 simplebench.main()
Save this code to a file, for example my_benchmark_script.py, and run it from your terminal:
python my_benchmark_script.py --rich-table.ops --progress
When you run this code, SimpleBench automatically handles timing the execution of addition_benchmark, collecting performance and memory statistics, and generating a report. By specifying Command-Line Options you can generate output in multiple formats, including rich text tables (like the one below), graphs, CSV files, and JSON reports.
Note that the docstring from the addition_benchmark function is automatically used as the description in the output.
For example, the command above produces the following rich table:
addition_benchmark
operations per second
A simple addition benchmark of Python's built-in sum function.
โโโโโโโโโโณโโโโโโโโโโโโโณโโโโโโโโโณโโโโโโโโโโณโโโโโโโโโโโโโณโโโโโโโโโโโโโณโโโโโโโโโโโโณโโโโโโโโโโโโโณโโโโโโโโโโโโโณโโโโโโโโโโโโโโณโโโโโโโโโโโโโณโโโโโโโโโ
โ โ โ โ Elapsed โ mean โ median โ โ โ โ โ std dev โ โ
โ N โ Iterations โ Rounds โ Seconds โ kOps/s โ kOps/s โ min Ops/s โ max kOps/s โ 5th kOps/s โ 95th kOps/s โ kOps/s โ rsd% โ
โกโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฉ
โ 1 โ 46701 โ 1 โ 0.32 โ 148.00 โ 149.00 โ 876.00 โ 153.00 โ 143.00 โ 151.00 โ 8.99 โ 6.08% โ
โโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโ
While this simple decorator is powerful, SimpleBench is built on a rich, extensible API. You can easily add more advanced features like setup and teardown functions, multi-dimensional parameterized benchmarks, and even create your own custom reporters. The library is designed to handle the boilerplate, so you can focus on writing and using your benchmarks.
The sections below provide more examples of some of these advanced features.