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.


