๐ WAYPOINT LESSON
Measurement discipline
โญโญโญ advancedโณ 16 min read๐ Lesson 130 of 169
Baseline, attribute, change one thing, verify โ with countable metrics.
Measure first: the discipline
Every performance claim in this module is graded the same way professionals verify: count the work, don't guess at the clock. Wall-clock on a shared machine is noisy; call counts, query counts, and allocation counts are facts.
The workflow:
- Establish a baseline โ the current numbers (latency, throughput, calls to the expensive thing).
- Attribute โ find where the work actually goes (next lesson: cProfile).
- Hypothesize โ "the O(nยฒ) membership test dominates", "we re-fetch the same rows 50 times".
- Change one thing.
- Verify โ re-measure the SAME metric. Keep the win, or revert.
Premature optimization has a precise meaning here: changing code before step 1
exists. timeit is the tool for micro-benchmarks (it disables GC and repeats
enough times to beat noise); time.perf_counter() is the wall-clock tool for
in-process timing; but for graded correctness of an optimization, count
operations โ a memoization win is calls: 13529 โ 39, not a fuzzy x2.