CPU profiling measures and attributes processor execution time across program call paths. A flame graph shows stack traces as horizontal function blocks: width represents total time, while vertical position represents stack depth. section 7.7
A practical profiling workflow is:
- Benchmark the implementation with Criterion. section 7.7
- Enable profiling with a five-second profile interval and generate a flame graph. section 7.7
- Inspect the widest blocks to locate functions consuming the most profiled CPU time. section 7.7
Fibonacci benchmark
A recursive Fibonacci benchmark measures about 157 nanoseconds, while memoization reduces it to about 44 nanoseconds. Profiling connects the measured runtime to the recursive call paths; iteration reaches about 5 nanoseconds. section 7.7
Listing 7.4 Fibonacci microbenchmark with CPU profiling
use pprof::criterion::{Output, PProfProfiler};
criterion_group! {
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = bench
}
criterion_main!(benches);
Use the Criterion configuration to attach pprof and emit a flame graph for the benchmark. section 7.7










