liveBook · Manning

Criterion.rs

As explained in Refactoring to Rust

Criterion is a Rust benchmarking library that performs statistically driven performance measurements.

Criterion.rs is a Rust crate for statistically driven performance measurements. It helps reduce misleading benchmark results by warming up implementations, collecting samples, and reporting timing ranges. Benchmarking Rust against Python can require a separate crate when the main crate is a PyO3 extension module. section 6.5 section 6.7

A basic Criterion benchmark requires these pieces:

  • Add Criterion under dev-dependencies so benchmark-only code is not linked into final artifacts. section 6.5
  • Declare a named Cargo bench target and set harness = false. section 6.7
  • Register a group with criterion_group! and criterion_main!, then measure work with c.bench_function and b.iter. section 6.7
  • Wrap measured values in criterion::black_box so the compiler does not optimize the work away. section 6.7

Comparing Rust and Python

A benchmark can call Python through PyO3 by acquiring the GIL with Python::with_gil, importing modules into a PyDict, and running code inside b.iter. One reported comparison measured pure Python at 25.415–25.842 microseconds and a Rust extension library at 21.746–22.314 microseconds for the same JSON-related task.

How Refactoring to Rust covers it

How deep it goes
Explains configuration, benchmark registration, iteration, optimization control, and sample output, but not the underlying statistical methodology.
Examples
Uses integer-width microbenchmarks and a pure-Python versus Rust-extension JSON calculation.
What it assumes
Assumes basic Rust and Cargo knowledge while introducing benchmark targets, dev-dependencies, and PyO3 initialization requirements.
What you can do after
Apply it via Cargo to build benchmark targets, measure functions, and compare reported timing ranges.
How it connects
Connects benchmarking with Cargo, dev-dependencies, PyO3, Python, and black_box.

Also explained in

Compare how 3 books explain Criterion.rs →

Related concepts

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.