liveBook · Manning

Cpu profiling

As explained in Fast by Default

CPU profiling measures and analyzes processor time consumed by program operations to identify computational hotspots.

CPU profiling measures where program execution spends processor time, exposing algorithmic inefficiencies, expensive computations, unexpectedly frequent calls, and hot-path work that may be cached. It turns a vague slowdown into an execution-time investigation. section 7.2 section 7.2.3

Profilers commonly sample execution at intervals or record every function call. They present flame graphs, call trees, or timing tables showing costly functions, callers, and call-stack depth. Trace the widest regions downward, then distinguish work performed directly from work performed by callees. section 7.2.3

A practical profiling workflow:

  • Profile realistic workloads during development, including production-sized data or representative concurrency where possible. section 7.2
  • Find functions that consume excessive time, execute too often, or keep important threads busy unnecessarily. section 7.2.3 section 7.4.2
  • Use line-level profiling after locating a slow function, and investigate operating-system traces when application-level evidence is insufficient. section 7.2.3 section 7.4.2

Reading cumulative and self time

A Python report shows serialize_response with 400 ms of cumulative time but only 5 ms of total self time. The function is mainly a gateway to expensive callees; investigation should continue down its call chain. A function with high self time is performing the costly work directly. section 7.2.3

Table 7.2 Backend profiling concerns, tools, and diagnostic patterns
Concern
Tools by language
What to look for
CPU hotspots
Python: cProfile[1], py-spy[2]
Node.js: --prof[3], clinic[4]
Go: pprof[5]
Java: async-profiler[6], Java Flight Recorder[7]
Functions taking longer than expected, functions call with unexpected frequency, work on hot paths that could be cached
Memory behavior
Python: tracemalloc[8], memory_profiler[9]
Node.js: heap snapshots[10]
Go: pprof heap
Java: VisualVM[11], Eclipse MAT[12]
Continuous growth without leveling off, objects that accumulate across requests, container memory approaching limits
Database queries
Query logging, EXPLAIN/EXPLAIN ANALYZE, Object-relational mapper (ORM) debug modes
N+1 patterns, sequential scans on large tables, missing indexes, queries returning more data than needed
Connection management
Pool metrics from your database driver
Pool exhaustion under load, connections held too long, connection leaks from error paths

Use the backend profiling matrix to choose language-appropriate CPU tools and distinguish CPU hotspots from memory, database, and connection investigations. section 7.2

How Fast by Default covers it

How deep it goes
Explains sampling, call-stack interpretation, cumulative versus self time, line-level follow-up, and platform tools without detailing profiler internals.
Examples
Uses Python cProfile output, py-spy flame graphs, repeated string comparisons, and a lingering progress timer as diagnostic examples.
What it assumes
Assumes familiarity with functions and application execution while teaching profiler output and investigation steps through concrete examples.
What you can do after
Diagnose CPU hotspots, distinguish direct work from costly callees, select platform tools, and target optimization or scheduling changes.
How it connects
Connects CPU profiling with flame graphs, call trees, line profiling, memory profiling, database query analysis, and operating-system tracing.

Also explained in

Compare how 2 books explain cpu 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.