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:

A simple benchmark example๐Ÿ”—
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:

Running the benchmark๐Ÿ”—
  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:

Output๐Ÿ”—
                                                               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.

Examples๐Ÿ”—