liveBook · Manning

Sample-based profiling

As explained in Fast by Default

A profiling technique that estimates execution-time distribution by periodically recording program execution state.

Sample-based profiling estimates where execution time goes by checking program state at regular intervals. It usually adds less overhead than recording every function call, making it a practical starting point for realistic performance measurements. section 7.3.1 section 7.2.3

Use a repeatable workflow to turn samples into actionable performance findings:

  • Profile representative user activity for at least 30 seconds: navigate screens, scroll lists, trigger animations, or perform common operations. section 7.3.1
  • Inspect the ranked call tree or flame graph, expanding the heaviest path through framework calls until it reaches application functions. section 7.3.1 section 7.2.3
  • Treat processor activity according to context: user-requested calculations may be expected, while activity during apparent idleness suggests unnecessary background work. section 7.3.1

Sampling gives an honest view with limited measurement drag, whereas trace-based profiling records every call and can alter timing. Use tracing when exact sequences are needed for a specific problem. section 7.3.1

Reading cumulative and internal time

Python cProfile can sort function statistics by cumulative time. A function such as serialize_response with 400 ms of cumulative time but 5 ms of internal time is mostly a gateway to slower callees; investigate its call chain rather than optimizing serialization immediately. High internal time identifies work performed directly by the function. section 7.2.3

Figure 7.5 An example call tree showing where time is spent. The top rows are system code you cannot change. Keep expanding until you reach your own functions at the bottom, which is where you focus your attention to improve performance.

A call-tree figure makes the investigation path concrete: move past costly system rows until your own functions become the optimization targets. section 7.3.1

How Fast by Default covers it

How deep it goes
Explains sampling, call-tree navigation, overhead tradeoffs, and timing interpretation, but defers exact call sequencing to trace profiling.
Examples
Uses mobile screen interactions, Android and iOS profilers, Python cProfile output, and a cumulative-time serialization example.
What it assumes
Assumes familiarity with application execution and function calls, while defining sampling, call trees, flame graphs, and timing columns.
What you can do after
Diagnose hotspots by collecting representative samples, following heavy call paths, and distinguishing direct work from expensive callees.
How it connects
Connects CPU profiling with call trees, flame graphs, trace-based profiling, and line-level profiling.

Also explained in

Compare how 2 books explain sample-based profiling →

Related concepts

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