Measure First, Optimize Second
advanced11 min readLesson 171 of 204
Guesswork optimizes the wrong 3%. Counting allocations, operations, and data movement turns performance into evidence.
The method
- Establish a baseline with a metric, not a feeling.
- Form one hypothesis ("the per-call allocation dominates").
- Change one thing.
- Re-measure the same metric.
- Keep or revert โ with numbers.
Wall-clock microbenchmarks are noisy and machine-dependent; this course grades the countable proxies professionals extract from profilers: allocation counts, bytes moved, comparisons, branch divergence detectable via deterministic patterns.
Where C++ programs actually lose time
In rough order of frequency: unnecessary copies (strings, vectors), unnecessary allocations (per-iteration buffers), wrong data layout (pointer chasing), wrong algorithm (O(n log n) vs O(n)), and only then micro-stuff like branch order.
Amdahl's law keeps you honest
If the sort is 90% of runtime, a 2x faster string format changes little. Profile-guided intuition: find the hot 10%, then optimize only inside it.