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.
1. Add microbench
Section titled “1. Add microbench”Run this in your project folder:
dart pub add dev:microbenchdev: makes it a dev dependency: you use it while developing, and it is
not shipped to the users of your package or app.
2. Write the benchmark
Section titled “2. Write the benchmark”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
mainis declaring things. Oncemainreturns, microbench measures every benchmark and prints the table. wordsis 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.
3. Run it
Section titled “3. Run it”dart run microbench benchmark/join_benchmark.dart --quickThe 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)4. Read the result
Section titled “4. Read the result”- Rate is how many times per second each benchmark ran: higher is
better.
49.2K ops/smeans about 49,200 runs per second. - The first row is the baseline. Every other row says how it compares
with it:
joinis 2.22 times faster thanStringBuffer, and+ operatoris 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.
5. Make it count
Section titled “5. Make it count”When you want numbers you can rely on, drop --quick, or ask for an even
longer run:
dart run microbench benchmark/join_benchmark.dart # up to 2 s per rowdart run microbench benchmark/join_benchmark.dart --thorough # up to 5 s per rowCheck that every version gives the same answer, because a fast wrong answer is no win:
dart run microbench benchmark/join_benchmark.dart --quick --verifyIf any benchmark in a group returns something different from the first
one, the run stops with an OutputMismatchError that shows both results.
6. Run all your benchmarks
Section titled “6. Run all your benchmarks”As you add more files, name each one something_benchmark.dart and keep
them in benchmark/. Then one command runs them all:
dart run microbench --quickRunning the file directly
Section titled “Running the file directly”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.
Several inputs in one row
Section titled “Several inputs in one row”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.
Where to go next
Section titled “Where to go next”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.