Benchmark design and release profiles
Quick answer
A benchmark measures a defined workload under recorded conditions. Rust debug and release builds can have different optimization settings, so comparing them as though they were identical is misleading.
Measure one change at a time
Keep input, correctness checks, compiler, target, machine and Cargo profile fixed. Warm up, repeat measurements and report a distribution rather than just the best observation. Exclude setup when measuring a kernel, or include it explicitly for end-to-end measurements. black_box can reduce unwanted optimization on a best-effort basis, not guarantee a precise benchmark or constant-time execution.
fn sum(xs: &[u32]) -> u64 { xs.iter().map(|&x| u64::from(x)).sum() }fn main() { assert_eq!(sum(&[]), 0); println!("{}", sum(&[1, 2, 3])); }Expected stdout (not executed):
6A correctness check must precede timing.
use std::{hint::black_box, time::Instant};fn main() { let data: Vec<u64> = (0..1000).collect(); let start = Instant::now(); for _ in 0..1000 { black_box(black_box(&data).iter().sum::<u64>()); } println!("{:?}", start.elapsed());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
Run cargo +1.85.0 run --release repeatedly; elapsed duration varies. No measured result supplied.Input creation is outside the measured interval; the loop includes iteration overhead.
Common mistake
Do not claim a speedup from one unrecorded run or compare different workloads.
Try it yourself
Record ten timings for one build and explain what setup is excluded.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Benchmark design and release profiles — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
One implementation is measured with input construction included and the other excludes it. What should change?
Benchmark design and release profiles — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn sum(xs: &[u32]) -> u64 { xs.iter().map(|&x| u64::from(x)).sum() } fn main() { assert_eq!(sum(&[]), 0); println!("{}", sum(&[1, 2, 3])); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Benchmark design and release profiles — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{hint::black_box, time::Instant}; fn main() { let data: Vec<u64> = (0..1000).collect(); let start = Instant::now(); for _ in 0..1000 { black_box(black_box(&data).iter().sum::<u64>()); } println!("{:?}", start.elapsed()); } ``` Which outcome or interpretation best matches this example?