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.
Test with several input sizes
Section titled “Test with several input sizes”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.
Benchmark async code
Section titled “Benchmark async code”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.
Compare your code with another package
Section titled “Compare your code with another package”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).
Verify outputs and select rows
Section titled “Verify outputs and select rows”dart run microbench --quick --verify benchmark/join_benchmark.dartdart run microbench --filter=join benchmark/join_benchmark.dartThe 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.
compare or benchmark?
Section titled “compare or benchmark?”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(),});benchmarkreads like a test, takessetupandteardown, and runs when therunSuitebody ends (or after a plainmainreturns).benchmarkin a plainvoid main()still gets the options passed todart run microbench, and so doescompareorrunComparisonoutside anyrunSuite.compareruns right away and returns the table, which helps when code should use the results. For many inputs, usematrix.