Skip to content
tezvyn:
Menu

Criterion: Statistical Benchmarking for Rust

Source: bheisler.github.ioMediumHow cards are made

Criterion isn't just a stopwatch; it's a statistical lab for your code. It provides stable performance metrics by running functions many times, letting you detect regressions and prove optimizations. The footgun is ignoring its statistical reports.

Why it exists

Simple timers give noisy results. Was your code faster, or was the OS just less busy for a moment? Criterion was built to answer this question with statistical confidence, moving beyond the limitations of basic benchmark runners like Rust's built-in libtest bencher.

The mental model

Think of Criterion as a science lab for your functions. A simple benchmark is like timing a sprint once with a phone. Criterion is like a full athletic combine: it warms up the runner, takes many measurements under controlled conditions, discards flukes, and produces a detailed report on their true average speed and consistency.

How it works

When you run cargo bench, Criterion takes over. For each benchmark, it performs a warm-up phase, then runs the code in a loop to collect thousands of timing samples. It analyzes this data to find the mean time and confidence intervals. Crucially, it saves these results. On subsequent runs, it compares the new statistics to the old ones and reports any statistically significant changes, flagging performance regressions automatically. It visualizes all this in detailed HTML reports.

When to use it

Use Criterion when performance is critical and "I think it's faster" isn't good enough. It's perfect for proving an optimization on a hot-path function, comparing the performance of two different algorithms side-by-side, or integrating into CI to automatically catch performance regressions before they merge.

When not to use it

Criterion excels at microbenchmarking—measuring small, self-contained functions. It is not a tool for system-wide, end-to-end profiling. If your function's performance is dominated by network calls, disk I/O, or database queries, a dedicated profiler will give you a more accurate picture of the real bottlenecks.

One canonical example

A classic use is comparing two implementations. You can create a benchmark group with two functions: one for a recursive Fibonacci solver and one for an iterative one. Criterion will run both, analyze their performance distributions, and generate a report that not only says which is faster but by how much, complete with graphs and statistical confidence levels.

Interview question

For which performance analysis task is Criterion LEAST effective?

  • a.Pinpointing bottlenecks caused by extensive database interactionsCorrect
  • b.Identifying a performance slowdown in a critical, self-contained function
  • c.Validating the speed improvement of a specific code optimization
  • d.Comparing the efficiency of two distinct CPU-intensive algorithms
Why?

Criterion excels at microbenchmarking CPU-bound functions, detecting regressions, and proving optimizations. It is explicitly stated as not suitable for system-wide profiling or issues dominated by I/O, such as database queries, which require a dedicated profiler.

Just read this? Test yourself on what you have been reading.

Read the original → bheisler.github.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on rust — each one lists the topics its interview covers.

See open roles