Skip to content

Working with comparison tables

Use compare for one set of variants and matrix when each variant accepts the same inputs. Reach for runComparison when a report needs several groups, a missing cell, or direct access to each measurement. The returned Comparison contains the data used by the built-in renderers.

Save this as benchmark/sum_benchmark.dart. The general implementation accepts any length. The two-element specialization only has a meaningful result in the first column.

import 'package:microbench/microbench.dart';
Future<void> main(List<String> args) => runMain(args, () async {
final inputs = [<int>[3, 7], List<int>.filled(1024, 1)];
final table = await runComparison(
title: 'Sum integers',
columns: ['2 items', '1024 items'],
metric: Metric.runtime,
groups: {
'Sum': (column) {
final items = inputs[column];
return [
SyncBenchmark.from('fold', () => items.fold<int>(0, (a, b) => a + b)),
if (items.length == 2)
SyncBenchmark.from('pair', () => items[0] + items[1])
else
const SkippedBenchmark('pair'),
];
},
},
);
for (final group in table.groups) {
for (final row in group.rows) {
final first = row.cells.first;
if (first != null) print('${row.name}: ${first.samples} timed batches');
}
}
});
Terminal window
dart run microbench --quick --verify benchmark/sum_benchmark.dart

The builder runs once per column. Return rows in the same order and with the same count each time. A SkippedBenchmark keeps the row aligned and leaves its measurement empty. Each column ranks its own rows. Here the first row supplies the baseline, and Metric.runtime favors the lower time.

A row’s cells holds the raw Measurement? values. Use table.cell(group, row, column) when you also need the baseline ratio, significance result, or best-cell marker. Check measurement before reading it in a general-purpose renderer. A ratio of 2 means the row scored twice as well as the baseline under the selected metric.

A TableRenderer writes a header, each completed group, and a footer. This lets long-running suites stream finished groups while later ones are measuring. To implement a new output format, start with the CSV renderer example. It handles skipped cells and exports the reported time and rate. For arbitrary user-supplied labels, add CSV quoting for commas, quotes, and newlines.

Register your renderer under a name to reach it from the command line: BenchmarkSettings(formats: {'csv': CsvTableRenderer()}).apply(...) makes --format=csv pick it, and --out=results.csv pick it from the extension. A registered name may replace a built-in.

Wrap a set of comparisons in BenchmarkSettings(...).apply(...) to supply a renderer, timing options, or filter throughout that block. Set onComparison to collect each finished table. An explicit option on a comparison takes precedence over its scoped default. The settings example collects two tables and inspects the rows retained by its filter.

For named comparisons that run immediately, put the calls inside runMain(args, ...) when they need --out or --save: timings and the row filter also apply to a plain main() that ran under the microbench runner, but writing files and comparing baselines need runMain. For a suite built from declarations, runSuite also runs the registered benchmarks at the end of the body.

All guides · Reports and regression checks