Skip to content

Your first benchmark

Compare three ways to join the same words and use the output to choose an implementation. You need a Dart project with a pubspec.yaml and Dart 2.19 or later. The harness warms up each variant and times repeated calls in batches so even a short operation can be measured.

Run this in your project folder:

Terminal window
dart pub add dev:microbench

dev: makes it a dev dependency: you use it while developing, and it is not shipped to the users of your package or app.

Create a folder named benchmark next to lib, and in it a file named join_benchmark.dart:

import 'package:microbench/suite.dart';
void main() {
final words = List.generate(1000, (i) => 'word$i');
group('Join 1000 words', () {
benchmark('StringBuffer', () {
final sb = StringBuffer();
for (final w in words) {
sb.write(w);
}
return sb.toString();
});
benchmark('join', () => words.join());
benchmark('+ operator', () {
var s = '';
for (final w in words) {
s += w;
}
return s;
});
});
}

If you have written tests with package:test, this will look familiar. What each part does:

  • benchmark(name, function) declares one thing to measure. microbench runs the function many times.
  • group(name, ...) collects benchmarks that compete with each other. They are printed together, and compared with the first one in the group.
  • Nothing runs while main is declaring things. Once main returns, microbench measures every benchmark and prints the table.
  • words is built once, outside the benchmarks, so building it is not part of what you measure.
  • Every benchmark returns its result. Without that, Dart may notice the result is never used and skip the work, and you would be timing nothing.
Terminal window
dart run microbench benchmark/join_benchmark.dart --quick

The runner compiles the file to native code and runs it. --quick keeps the run short while you experiment. Your timings will vary.

Join 1000 words Rate
StringBuffer 49.2K ops/s
join 109.24K ops/s [best] (2.22x faster)
+ operator 3.58K ops/s (13.74x slower)
  • Rate is how many times per second each benchmark ran: higher is better. 49.2K ops/s means about 49,200 runs per second.
  • The first row is the baseline. Every other row says how it compares with it: join is 2.22 times faster than StringBuffer, and + operator is 13.74 times slower.
  • [best] marks the fastest row.

So for this job join wins, and building a string with + in a loop is far slower. Reading the results explains every mark you may see.

When you want numbers you can rely on, drop --quick, or ask for an even longer run:

Terminal window
dart run microbench benchmark/join_benchmark.dart # up to 2 s per row
dart run microbench benchmark/join_benchmark.dart --thorough # up to 5 s per row

Check that every version gives the same answer, because a fast wrong answer is no win:

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

If any benchmark in a group returns something different from the first one, the run stops with an OutputMismatchError that shows both results.

As you add more files, name each one something_benchmark.dart and keep them in benchmark/. Then one command runs them all:

Terminal window
dart run microbench --quick

dart run benchmark/join_benchmark.dart works too, with the default settings. Dart only gives a program’s arguments to main, so to pass options that way (dart run benchmark/join_benchmark.dart --quick), hand them to runSuite:

Future<void> main(List<String> args) => runSuite(args, () {
// the same group and benchmarks as above
});

dart run microbench passes options through an environment variable instead, so a plain main needs nothing there. The same goes for every API, not just benchmark: a void main() calling compare or runComparison honors --quick, --filter and --format through the runner with no wrapper at all.

To compare implementations across several payload sizes side by side, give each row the runs for each column:

benchmarkColumns('Base-16 / convertlib', {
'1MB': () => toHex(megabyte),
'1KB': () => toHex(kilobyte),
'32B': () => toHex(tiny),
}, sizes: [1 << 20, 1 << 10, 1 << 5]);

With sizes, the cells show throughput; without, runs per second. Every row of the suite must use columns once one does.

Learn how the harness ranks rows and what the spread means in Reading the results. To try async work or more than one input size, continue with Designing comparisons.

All guides