Skip to content

Designing comparisons

Question Start with
How do two functions perform on one input? compare with one closure per variant
Does the ranking change with input size? matrix with one column per case
Does each variant need setup or teardown? benchmark declarations inside a group
Do some variants support only certain inputs? Working with comparison tables

The examples below are fragments for an async body passed to runMain. Import package:microbench/microbench.dart for immediate comparisons, or package:microbench/suite.dart when using declarations.

Use matrix: every variant runs on every case, with one column per case.

await matrix<List<int>>(
'Sum a list',
cases: [List.filled(100, 1), List.filled(10000, 1)],
label: (list) => '${list.length} items',
variants: {
'for loop': (list) {
var sum = 0;
for (var i = 0; i < list.length; i++) {
sum += list[i];
}
return sum;
},
'fold': (list) => list.fold<int>(0, (a, b) => a + b),
},
);
Sum a list 100 items 10000 items
for loop 17.11M ops/s [best] 212.24K ops/s [best]
fold 3.32M ops/s (5.16x slower) 34.56K ops/s (6.14x slower)

The cases are built once, before timing starts.

Measure speed in bytes (or anything) per second

Section titled “Measure speed in bytes (or anything) per second”

Give a size, the amount of work in one run. With a size, the table shows throughput instead of runs per second:

await matrix<List<int>>(
'Copy bytes',
cases: [List.filled(1024, 0), List.filled(1 << 20, 0)],
size: (bytes) => bytes.length, // bytes per run
variants: {
'List.of': (bytes) => List.of(bytes),
'toList': (bytes) => bytes.toList(),
},
);

For other units, add metric: Metric.perSecond('chars'), or 'items', or anything else.

Return the Future; microbench waits for it:

await compare('Wait', {
'Future.value': () => Future.value(1),
'Future.microtask': () => Future.microtask(() => 1),
});

Give each variant equivalent starting data

Section titled “Give each variant equivalent starting data”

Build reusable inputs before timing. For an operation that mutates its input, decide whether copying belongs in the operation you want to measure. The sorting example includes the cost of copying in both variants. Without a fresh copy, later runs would sort an already-sorted list.

For resources that live for the whole measurement, attach setup and teardown to a declaration or override them in a benchmark class. They run once per session. For async work, return the future that completes after the operation finishes. Sorting example and benchmark class example.

Put both in the same compare, yours first so the others are compared with it:

await compare('Base64 encode 1 KB', {
'mine': () => myEncode(bytes),
'dart:convert': () => base64.encode(bytes),
});

Add the other package as a dev dependency too (dart pub add dev:name).

Terminal window
dart run microbench --quick --verify benchmark/join_benchmark.dart
dart run microbench --filter=join benchmark/join_benchmark.dart

The filter is a regular expression matched against title / group / row. A group name that matches keeps every row in that group. Filtering out the original baseline hides its ratios. Verification compares the retained implementations, so keep at least two variants when checking agreement.

They make the same table. These two are equivalent:

group('join', () {
benchmark('join', () => words.join());
benchmark('StringBuffer', () => (StringBuffer()..writeAll(words)).toString());
});
await compare('join', {
'join': () => words.join(),
'StringBuffer': () => (StringBuffer()..writeAll(words)).toString(),
});
  • benchmark reads like a test, takes setup and teardown, and runs when the runSuite body ends (or after a plain main returns).
  • benchmark in a plain void main() still gets the options passed to dart run microbench, and so does compare or runComparison outside any runSuite.
  • compare runs right away and returns the table, which helps when code should use the results. For many inputs, use matrix.

All guides · Getting trustworthy numbers