Measure a Representative Workload
Core idea
Use a fixed dataset matching the contract, verify results first, then measure repeated parsing and aggregation on the same bytes. Record interpreter, platform, input count, input size, repeat count and units. The fixture benchmark uses 1000 synthetic orders and reports five samples of elapsed seconds per complete parse-and-summary call. Compare baseline and candidate only after checking result equality. A bounded dataset describes one workload, not production latency, memory or throughput. Separate parsing cost from aggregation if the goal is to diagnose one stage.
values = [i % 100 for i in range(1000)]def baseline(xs): total = 0 for x in xs: total += x return totalprint(baseline(values) == sum(values))print(sum(values))The same deterministic workload gives the same result with both implementations; this is not yet a speed claim.
Expected output:
True49500Repeat a small measurement
from timeit import repeatvalues = list(range(100))samples = repeat(lambda: sum(values), number=20, repeat=5)print(len(samples))print(all(t >= 0 for t in samples))The check validates measurement structure, not exact elapsed durations. Divide each elapsed sample by number to express time per call.
Expected output:
5TrueCommon mistake
Never assert that an optimization is faster from one noisy run. timeit disables garbage collection by default during timing; record that choice. Do not confuse the microbenchmark with command startup or production I/O.
Practice
Run benchmark.py twice. Record the five per-call measurements and median. Change the dataset to 100 records and compare separately; explain why neither run proves production performance.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Performance review
Check your understanding of performance review.
What is printed?
print(sum(i%10 for i in range(100)))What is printed?
from timeit import repeatprint(len(repeat(lambda: 1+1, number=2, repeat=3)))What must precede a speed comparison?
What does this benchmark establish?