liveBook · Manning

Cargo bench

As explained in Refactoring to Rust

cargo bench is a Cargo command that builds and runs a package's benchmarks.

cargo bench builds and runs a package's benchmarks. With Criterion configured, it also runs unit tests, warms caches, collects samples, and reports estimated iteration times with confidence intervals. section 6.5

A typical Criterion benchmark requires:

  • black_box around computed results so the compiler does not optimize away unused work. section 6.5

Run benchmarks

From the package directory, run cargo bench. Criterion warms up for three seconds, collects samples for about five seconds, and prints a timing interval such as [257.13 ps, 261.71 ps, 266.79 ps].

How Refactoring to Rust covers it

How deep it goes
Explains invocation and Criterion output, while deferring benchmark implementation details to separate source files.
Examples
Uses u8 and u128 arithmetic benchmarks and compares pure Python with a Rust extension library.
What it assumes
Assumes a Cargo package with Criterion configured through Cargo.toml and benchmark code under benches/.
What you can do after
Operate/configure it to run benchmarks and interpret Criterion's timing intervals and sample estimates.
How it connects
Connects Cargo with Criterion, benchmark tests, unit tests, and the built-in testing harness.

Also explained in

Compare how 2 books explain cargo bench →

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.