Example: one benchmark, several parameter combinations; comparisons across parameters¶
In this demo, we show
-
how to run a parametrized benchmark: time a matrix multiply,
a @ b, for square arrays of several sizes, represented as numpy arrays and torch tensors. -
how to arrive at a comparison verdict across "variants": is matrix multiply in NumPy faster or slower than PyTorch's?
The first part follows the UC-01 shape, and second part is a UC-02 benchmark: the same function, run once with each library, with the rounds interleaved.
Install the required dependencies¶
python -m venv .venv && . .venv/bin/activate
pip install benchx[parquet]
pip install numpy
pip install torch --index-url https://download.pytorch.org/whl/cpu # the CPU build
Run a quick version¶
By default, benchx uses pyperf's defauls: 20 worker processes x 3 values each, and 0.1s calibration.
In this demo, we use a quick-and-dirty version, so that the run time is smaller but the results are noisy
$ export QUICK="--processes 2 --values 2 --min-time 0.01"
$ bx bench bench_matmul.py $QUICK
measuring bench_matmul.py on .
precision: 2 processes x 2 values (config: examples/matmul-params/benchx.conf.json)
case median mean ± sd min .. max n status
-------------------------------- -------- ------------------- -------------------- - -------
bench_matmul [n=10, xp='numpy'] 1.519 us 1.522 us ± 8.396 ns 1.516 us .. 1.534 us 4 success
bench_matmul [n=10, xp='torch'] 1.454 us 1.453 us ± 19.98 ns 1.43 us .. 1.477 us 4 success
bench_matmul [n=50, xp='numpy'] 6.828 us 6.947 us ± 493.2 ns 6.52 us .. 7.613 us 4 success
bench_matmul [n=50, xp='torch'] 11.24 us 11.24 us ± 1.496 us 9.938 us .. 12.54 us 4 success
bench_matmul [n=100, xp='numpy'] 38.38 us 38.41 us ± 148.6 ns 38.27 us .. 38.62 us 4 success
bench_matmul [n=100, xp='torch'] 33.77 us 33.78 us ± 102.3 ns 33.68 us .. 33.92 us 4 success
bench_matmul [n=200, xp='numpy'] 205.1 us 205.2 us ± 1.207 us 204.1 us .. 206.5 us 4 success
bench_matmul [n=200, xp='torch'] 206.1 us 206.2 us ± 999.7 ns 205.3 us .. 207.4 us 4 success
16 result(s) -> examples/matmul-params/out/bench-20261130T233017Z (order sha256:37b04423cac4)
run-key: bench-20261130T233017Z
examples/matmul-params/out/home/store.parquet: ingested 16, duplicate 0
The output shows a row per combination, named by the function and its arguments. It also shows paths to the benchx configuration file, and the local store, a parquet file which contains detailed data from the benchmarking run.
As usual (see the basic tutorial),
bare result files are in out/, and out/home/store.parquet is the store.
Note that bx reports twice as many results as rows: each case also records load-average---
roughly, the load average is how many processes were running or waiting for a CPU over at the start of the benchmark
run. A value well above the CPU core count means the machine was busy, so the timings may be noisy.
Choosing cases¶
We can select specific cases by either giving exact parameter values or a regex. Note that since we are parametrizing the benchmark with string arguments, we have to use the quoted strings in CLI.
$ bx bench bench_matmul.py bench_matmul # a function's name: all eight
$ bx bench bench_matmul.py "bench_matmul(100, 'torch')" # one combination
$ bx bench bench_matmul.py --filter "'torch'" # a regex: the four torch cases
A regex is searched in bench_matmul(10, 'torch'), the function's name and the argument
values (not their names). --filter 'bench_matmul$' is therefore refused, since $ leaves
no room for the arguments; the refusal says so.
Comparing numpy and torch¶
Suppose now we ask: is PyTorch matmul faster than NumPy matmul for the same matrix size?
We can of course, visually inspect the output above, and try to guesstimate the spread of the results
(shown in the min..max column).
benchx however provides a better way to perform a comparison and run a statistical analysis to come up with
a comparison verdict (faster, slower or indeterminate). The relevant CLI command is bx compare.
A first attempt (that fails)¶
We have already run benchmarks for both numpy and torch, can we use them to run the comparison via bx compare?
First, we need to find out the run key. We did not specify it explicitly, thus it has been automatically
generated by bx bench, from the name of the benchmark and the timestamp:
$ bx head
examples/matmul-params/out/home/store.parquet: 16 result(s); latest 10, newest first
row started run-key workload quantity status median revision project
15 2026-11-30 23:30:32 bench-20261130T233017Z bench_matmul load-average success 2.11 5308f3ec84 local/runnervm3lb7s
14 2026-11-30 23:30:32 bench-20261130T233017Z bench_matmul wall-time success 0.0002061 s 5308f3ec84 local/runnervm3lb7s
13 2026-11-30 23:30:32 bench-20261130T233017Z bench_matmul load-average success 1.95 5308f3ec84 local/runnervm3lb7s
12 2026-11-30 23:30:32 bench-20261130T233017Z bench_matmul wall-time success 0.000205111 s 5308f3ec84 local/runnervm3lb7s
11 2026-11-30 23:30:28 bench-20261130T233017Z bench_matmul load-average success 1.95 5308f3ec84 local/runnervm3lb7s
10 2026-11-30 23:30:28 bench-20261130T233017Z bench_matmul wall-time success 3.37657e-05 s 5308f3ec84 local/runnervm3lb7s
9 2026-11-30 23:30:28 bench-20261130T233017Z bench_matmul load-average success 1.95 5308f3ec84 local/runnervm3lb7s
8 2026-11-30 23:30:28 bench-20261130T233017Z bench_matmul wall-time success 3.83838e-05 s 5308f3ec84 local/runnervm3lb7s
7 2026-11-30 23:30:24 bench-20261130T233017Z bench_matmul load-average success 1.77 5308f3ec84 local/runnervm3lb7s
6 2026-11-30 23:30:24 bench-20261130T233017Z bench_matmul wall-time success 1.12351e-05 s 5308f3ec84 local/runnervm3lb7s
The run key is listed in the fourth column of the output.
Next, we supply the following parameters to bx compare:
- we compare across variants: same function, varying values of its parameter:
--profile variants; - we vary the
xpparameter and compare for each value of the other parametern:--vary xp; - we ask whether the contender PyTorch is measurably faster than the baseline NumPy:
--baseline "'numpy'"; Note that the double quotes here are not a typo: in Python, we parametrize the benchmark function with strings, thus we need to keep the quotes in the CLI invocation.
Taking it all together, we run
$ RUN=$(bx head -n 1 | awk 'NR==3 {print $4}')
$ bx compare --profile variants --run-key $RUN --vary xp --baseline "'numpy'"
run-key bench-20261130T233017Z profile variants baseline 'numpy' contender 'torch'
failed invariants (1)
alternating-slots: a result has no slot: give every bx bench call --slot; make one bx bench call per value, each with --run-key, --round and --slot, or run bx compare TARGET --profile variants
which fails with a mysterious error message. What is going on here?
A proper comparison requires an alternating run¶
Here we are facing a design constraint of benchx: to produce a comparison verdict, it needs to
run the comparison sides multiple times, and interleave the runs. This way, it can estimate the noise
and control for possible drift in the benchmarking environment.
To run the comparison, we need to explicitly to provide the --rounds R argument, where each
side of the comparison will be run R times. In fact, to compare the sides A and B, it
runs a sequence ABABABAB or ABBAABBA, of 2R runs in total, where it places
the individuals runs for the sides into alternating slots.
From the run-to-run variations, it estimates the noise. To arrive to a
comparison verdict (regressed, improved, indeterminate), it compares the measured
difference between A and B to the noise estimate.
The minimal number of rounds is four, since otherwise the noise estimate is too unreliable.
Setting a smaller number of rounds will result in INDETERMINATE (insufficient-rounds)
as a comparison verdict.
Increasing the number of rounds improves the quality of the statistical estimates, at
the expense of longer run times.
Taking this all together:
$ export QUICK="--processes 2 --values 2 --min-time 0.01"
bx compare bench_matmul.py --profile variants --vary xp --rounds 5 $QUICK
examples/matmul-params/out/home/store.parquet
round 1/5 xp='numpy': 8 result(s)
round 1/5 xp='torch': 8 result(s)
round 2/5 xp='torch': 8 result(s)
round 2/5 xp='numpy': 8 result(s)
round 3/5 xp='numpy': 8 result(s)
round 3/5 xp='torch': 8 result(s)
round 4/5 xp='torch': 8 result(s)
round 4/5 xp='numpy': 8 result(s)
round 5/5 xp='numpy': 8 result(s)
round 5/5 xp='torch': 8 result(s)
run-key compare-20261130T233039Z profile variants baseline 'numpy' contender 'torch'
REGRESSED (1)
bench_matmul [n=50] wall-time effect +50.48% noise 1.85% rounds=5
IMPROVED (2)
bench_matmul [n=10] wall-time effect -4.10% noise 0.26% rounds=5
bench_matmul [n=100] wall-time effect -10.53% noise 0.16% rounds=5
NO CHANGE DETECTED (1)
bench_matmul [n=200] wall-time effect +0.66% noise 0.13% rounds=5
The result is one unit per size, with numpy as the baseline and torch as the contender: the effect is
torch's median time relative to numpy's, so REGRESSED means torch took longer, and the noise is taken
across the five rounds. The units are grouped by verdict.
On one specific laptop, the report was that torch is on par with numpy at N=10 and is faster for larger N.
In general, for our quick-and-dirty setting, $QUICK, results are expected to show fairly wild variations or even be borderline
noise. A real comparison will need to use at least the default settings or actually larger number of rounds and values.
Show all comparison results, as a table¶
The output above only shows "effects" and does not show actual timings. To show both, we could have
added --tables --all to the bx compare call. Given that we did not add it, we can rerun the
comparison (but not the benchmarks themselves!) in the so-called read mode, where it pulls the
data to compare from the store.
First, instead of the benchmark suite, it needs the run key for the precomputed data.
Then, we give it the same --profile variants --vary xp arguments to specify what to
compare, and also we tell it which side is the baseline, --baseline "'numpy'"
Taking it all together,
$ RUN=$(bx head -n 1 | awk 'NR==3 {print $4}')
$ bx compare --run-key $RUN --profile variants --vary xp --baseline "'numpy'" --tables --all
run-key compare-20261130T233039Z profile variants baseline 'numpy' contender 'torch'
REGRESSED (1)
bench_matmul [n=50] wall-time effect +50.48% noise 1.85% rounds=5
IMPROVED (2)
bench_matmul [n=10] wall-time effect -4.10% noise 0.26% rounds=5
bench_matmul [n=100] wall-time effect -10.53% noise 0.16% rounds=5
NO CHANGE DETECTED (1)
bench_matmul [n=200] wall-time effect +0.66% noise 0.13% rounds=5
For local/runnervm3lb7s xp='numpy' (5 rounds):
bench_matmul ok
===== =============
n
----- -------------
10 1.53±0.01μs
50 6.54±0.02μs
100 38.0±0.07μs
200 205±0.4μs
===== =============
For local/runnervm3lb7s xp='torch' (5 rounds):
bench_matmul ok
===== =============
n
----- -------------
10 1.47±0.01μs
50 9.81±0.3μs
100 34.1±0.2μs
200 206±0.3μs
===== =============
| Change | Before ['numpy'] | After ['torch'] | Ratio | Noise | Verdict | Benchmark (Parameter) |
|----------|--------------------|-------------------|---------|---------|--------------------|-------------------------|
| - | 1.53±0.01μs | 1.47±0.01μs | 0.96 | 0.26% | improved | bench_matmul(10) |
| + | 6.54±0.02μs | 9.81±0.3μs | 1.50 | 1.85% | regressed | bench_matmul(50) |
| - | 38.0±0.07μs | 34.1±0.2μs | 0.90 | 0.16% | improved | bench_matmul(100) |
| | 205±0.4μs | 206±0.3μs | 1.01 | 0.13% | no change detected | bench_matmul(200) |
which first repeats the same information it gave without --tables, and then emits
the summary tables.
A manual loop + "read mode" comparisons.¶
Above we used bx compare in the so-called measure mode, where it ran the benchmarks under the comparison schedule. An alternative, more
manual, way is to run a loop over bx bench calls and then invoke bx compare in the read mode, as before.
For the first step, we need to handle alternating the sides manually: Each call runs one library,
with --filter, as one side of the session; we also need to specify the same run key for all calls,
via the --run-key argument.
The previous bx compare call sequence used the --rounds argument to arrange the individual calls into
the comparison sequence. Here we manually assemble the comparison sequence ABABABAB using the
--slots argument (note how numpy runs are assigned even slots and torch runs are assigned the
odd slots)
Taking this all together, we call
for r in 0 1 2 4; do
bx bench bench_matmul.py --filter "'numpy'" --run-key mm --round $r --slot $((2*r)) $QUICK
bx bench bench_matmul.py --filter "'torch'" --run-key mm --round $r --slot $((2*r+1)) $QUICK
done
The output (not shown) will be somewhat noisy because every bx bench call prints out its measurements.
Once the data is computed, we run the comparison in the read mode by specifying the --run-key RUN_KEY
$ bx compare --run-key mm --profile variants --vary xp --baseline "'numpy'"
The resulting output will be the same as the measure mode comparison (since we used quick-and-dirty settings, results are not reliable and may thus differ though).
Final remarks¶
In the second part of this example, we demonstrated two ways of running a comparison: the measurement mode,
where bx compare runs benchmarks itself, and the read mode, where we run benchmarks in a manual loop.
Why would one use the second variant over the measurement mode, as it looks to be more work for no gain?
For the variants comparison it is indeed unnecessary. However, benchx supports several other comparison profiles:
revisions (where you run comparisons across the history of a project) and environments (where you hand
bx compare two prepared virtual environments). These kinds of tasks involve building the project or activating
environments---and benchx does not do either. This is where the read mode comparisons come in handy. See the
pandas-asv worked example for an
end-to-end demo.