4.5.24 · D3Software Engineering

Worked examples — Performance profiling — CPU, memory, I - O profiling

3,590 words16 min readBack to topic

The scenario matrix

Before touching numbers, let us list the shapes a profiling problem comes in. Think of this table as a checklist: every cell must get at least one fully-worked example below, so you never meet a scenario cold.

# Case class What makes it tricky Example
A Big slice ( large) optimization pays off Ex 1
B Small slice ( small) Amdahl says don't bother Ex 2
C Limit ceiling / best-case speedup Ex 3
D Degenerate or formula edge behaviour Ex 4
E Sampling: how many samples? Binomial error, solve for Ex 5
E′ Sampling worst case maximal → most samples Ex 5b
F Sampling: tiny hotspot error explodes for small Ex 6
G Wall ≫ CPU (I/O-bound) the profiler "lies" Ex 7
H Wall ≈ CPU (CPU-bound) trust the profiler Ex 7
I Self vs cumulative time wrapper vs real worker Ex 8
J Real-world word problem translate English → Ex 9
K Exam twist (multi-stage, combine laws) chain two optimizations Ex 10

Let us first pin down the symbols so nothing is used before it is defined.


Case A — the big slice pays off


Case B — the small slice: Amdahl says skip it


Case C — the limit : your best-case ceiling


Case D — degenerate inputs and


Case E — sampling: how many samples do I need?

Now switch tools. A sampling profiler peeks at "what's running now?" every so often. Each peek is a coin flip: landed-in-our-function (heads) or not (tails). With flips and true fraction , the count of heads follows the Binomial distribution, and the spread of our estimate is the standard error — the typical distance between our measured fraction and the truth:


Case E′ — the worst case: needs the most samples


Case F — sampling breaks down for a tiny hotspot


Case G & H — wall ≫ CPU vs wall ≈ CPU (does the profiler lie?)

Recall the elapsed-time split, where is real clock time you'd measure with a stopwatch, is time cores were actually computing (user + sys), and the rest is waiting (disk, network, locks, sleep):


Case I — self time vs cumulative time


Case J — real-world word problem


Case K — exam twist: chaining two optimizations


Recall Which time do you sort by to find hotspots?

Self time — why? ::: Self (exclusive) time is the work inside a function; cumulative can be inflated by a thin wrapper whose children do the real work (Ex 8).

Recall Wall 10 s, CPU 1 s — I/O or CPU bound, and does the CPU profiler help?

Answer ::: I/O-bound (10% utilization); the CPU profiler explains only 1 s and effectively lies — profile I/O instead (Ex 7).

Recall Which hotspot fraction needs the

most samples for a fixed accuracy? Answer ::: , because peaks there; size for that worst case and you never under-sample (Ex 5b).

Recall Why can't you trust a sampled function that got 3 of 1000 hits?

Answer ::: Its relative standard error is ~58% — tiny slices are where sampling is least reliable (Ex 6).