liveBook · Manning

Profiling

As explained in Latency

CPU profiling is the measurement and attribution of processor execution time across program call paths.

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

How Latency covers it

How deep it goes
Explains flame-graph structure and interpretation, then stops short of broader profiling caveats or implementation internals.
Examples
Uses recursive, memoized, and iterative Fibonacci implementations with Criterion timings and a generated flame graph.
What it assumes
Assumes a Criterion microbenchmark and basic familiarity with functions, call stacks, and benchmark timings.
What you can do after
Diagnose expensive call paths using Criterion, pprof, profile intervals, and flame-graph widths.
How it connects
Connects CPU profiling with benchmarking, flame graphs, call stacks, memoization, and algorithmic optimization.

Also explained in

Compare how 11 books explain profiling →

Related concepts

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