Basic Benchmark๐
This tutorial demonstrates how to create a simple benchmark using SimpleBench, run it, and generate a report.
The minimal code required to create and run a benchmark using SimpleBench is creating a script that defines a function to be benchmarked with @simplebench.benchmark and that calls simplebench.main() and then running it.
When you run the code below, SimpleBench automatically runs the benchmark function 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 and select which statistics to report.
It runs using defaults setting for number of iterations, warmup runs, etc. You can customize these settings via parameters set on the @simplebench.benchmark decorator.
1"""A basic benchmark."""
2import simplebench
3
4
5@simplebench.benchmark
6def addition_benchmark():
7 """A simple addition benchmark of Python's built-in sum function."""
8 sum(range(1000))
9
10
11if __name__ == "__main__":
12 simplebench.main()
Save this code to a file, for example basic_benchmark.py, and then run it from
your terminal:
python basic_benchmark.py --rich-table.ops --progress
This will produce output similar to the following:
addition_benchmark
operations per second
A simple addition benchmark of Python's built-in sum function.
โโโโโโโโโโณโโโโโโโโโโโโโณโโโโโโโโโณโโโโโโโโโโณโโโโโโโโโโโโโโณโโโโโโโโโโโโโโโโณโโโโโโโโโโโโโณโโโโโโโโโโโโโณโโโโโโโโโโโโโณโโโโโโโโโโโโโโณโโโโโโโโโโโโโโโโโณโโโโโโโโโ
โ โ โ โ Elapsed โ โ โ โ โ โ โ โ โ
โ N โ Iterations โ Rounds โ Seconds โ mean kOps/s โ median kOps/s โ min kOps/s โ max kOps/s โ 5th kOps/s โ 95th kOps/s โ std dev kOps/s โ rsd% โ
โกโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฉ
โ 1.0 โ 41789 โ 1 โ 0.30 โ 141.00 โ 140.00 โ 22.80 โ 153.00 โ 138.00 โ 151.00 โ 9.28 โ 6.59% โ
โโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโดโโโโโโโโโ
A detailed explanation of the output format and displayed statistics shown in this example report can be found in the Rich Table Report section of the documentation.
Note
The command shown above includes the --progress option to
display a progress bar while the benchmark is running. This option is not
required to run the benchmark, and can be omitted if you do not wish to see
the progress bar. It is not included in the expected output file since it
produces dynamic output that changes as the benchmark runs and is removed
from the terminal once the benchmark completes.