Comparing Implementations for Efficiency
Empirical Performance Measurement: Fair Tests and Careful Conclusions
Empirical Performance Measurement uses observations from running programs to compare their resource use under specified conditions. You use it when choosing between implementations that produce the same required result. A timing experiment answers a concrete question about particular inputs, software settings, and hardware. It provides evidence about those conditions; it does not establish performance for every possible input.
Decide what the measurement includes
Begin with the decision you need to make. Are you comparing one lookup after a data structure is ready, or the complete time to build that structure and answer a request? Both questions are useful, but they require different timing boundaries.
Choose inputs that reflect the intended workload, including their sizes and relevant characteristics. Two collections with the same number of entries can still produce different timings if their ordering, repeated values, or query patterns differ. Give both implementations the same logical inputs and verify that their outputs satisfy the same correctness requirements.
Decision | Include in the timed region | Usually prepare separately |
|---|---|---|
Compare ready-to-use operations | The same sequence of operations | Input generation and structure construction |
Compare complete jobs | Required loading, construction, and processing | Test-data creation unrelated to the job |
Compare warmed request handling | Requests after the chosen warm-up | Startup and excluded warm-up work |
Use the same timing boundary for both implementations. If an operation changes its input, restore an equivalent starting state before each trial. Otherwise, later trials may perform different work.
Collect timings that you can interpret
Use a monotonic clock appropriate for measuring elapsed time. Keep the machine, software versions, compiler settings, and competing background activity as consistent as practical. Record these settings so someone else can understand the comparison.
Very short operations can be difficult to time individually. Run a batch of k operations and divide the elapsed batch time by k:
This is the average time per operation within that batch, including any costs still inside its boundary. Batching reduces the relative impact of reading the clock, but it does not automatically remove loop overhead. Subtract separately measured overhead only when an additive, unchanged overhead model is justified.
For warmed execution, run excluded warm-up work before collecting measurements. Warm-up allows effects such as runtime compilation and recently accessed data remaining in fast memory to settle. For a startup-sensitive task, measure startup instead of silently excluding it. Ensure computed results are used observably so optimization does not remove the intended work.
Repeat the measurement. Report the number of trials, a suitable summary such as the median, and the observed spread. After sorting the timings, the median is the middle observation for an odd number of trials, or the average of the two middle observations for an even number of trials. It reduces the influence of an unusually long trial but does not explain why that trial occurred.
Compare results on a shared basis
Speedup is the reference time divided by the candidate time for the same work. Using comparable summary times gives:
A speedup above 1 favours the candidate. Speedup is dimensionless because the time units cancel. Percentage time reduction uses the reference time as its denominator; it is a different quantity from the speedup factor.
A small difference compared with the run-to-run spread deserves further investigation. Repeat under controlled conditions and inspect the individual timings rather than claiming a decisive advantage from one favourable trial. Measure several relevant input sizes before making a recommendation across a workload.
State conclusions within the tested workload and environment. Timing evidence alone also says nothing definite about an implementation's memory use; measure that resource separately when it matters.
What to remember
- Define the workload, correctness requirements, and timing boundary first.
- Repeat comparable trials and report both typical time and variability.
- Distinguish time per operation, speedup, and percentage time reduction.
- Include startup or setup whenever the intended use requires it.
Example: Comparing lookup timings and complete jobs
A product catalogue service is testing two lookup implementations. A batch is a group of operations measured together; each batch here contains the same sequence of 5000 read-only lookups. Both implementations return the required results. Construction and input preparation are excluded from the lookup timer, warm-up is complete, and execution order alternates on the same machine.
The five batch times for A are 62, 60, 63, 61, and 59 ms. For B, they are 46, 44, 45, 47, and 43 ms. Treat remaining timing overhead as negligible for this example.
a) Use each implementation's median batch time to estimate its time per lookup. Give both results in microseconds to one decimal place.
Sort the times to identify the middle observation in each set of five. A's sorted times are 59, 60, 61, 62, and 63 ms, so its median is ________. B's sorted times are 43, 44, 45, 46, and 47 ms, so its median is ________.
Divide each median batch time by 5000. Convert milliseconds to microseconds using 1 ms=1000 μs:
The estimated times per lookup are 12.2 microseconds for A and 9.0 microseconds for B. These values are batch averages calculated from the median batch durations, rather than medians of individually timed lookups.
b) Using A as the reference, calculate B's speedup factor to two decimal places and percentage time reduction to one decimal place.
The median batch-time difference is ________. Divide the reference time by the candidate time for speedup:
For percentage time reduction, divide the difference by the reference time. The unrounded fraction saved is approximately ________:
B has a speedup factor of 1.36 and a time reduction of 26.2% for the measured lookup batches. Both comparisons use the same work and the same reference implementation.
c) A separate complete-job test must construct a catalogue and then perform one batch of 5000 lookups. Construction takes 18 ms for A and 90 ms for B. Assume construction time and the median lookup-batch time add exactly, with no other costs. Which implementation has the smaller estimated complete-job time, and by how many milliseconds?
The end-to-end time is the time for all stages included in the complete job. Here, construction is included once before the lookup batch, so add it to each implementation's median lookup time:
The estimated totals are ________ for A and ________ for B. Subtract the smaller total from the larger total to find the difference:
A has the smaller estimated complete-job time, by 56 milliseconds. B wins the lookup-only comparison, but its larger construction cost reverses the choice for this one-batch job under the stated additive model.
A team compares two implementations of a document-processing service. Match each performance metric to the description of what it measures during a specified test interval.
Number of jobs completed per unit of measurement time
Total time the CPU cores spend running the program's instructions, summed across cores (excluding time spent waiting)
Wall-clock duration between the selected start and finish events
Largest amount of storage the program holds simultaneously at any single instant during the measured interval
Peak memory use
Elapsed time
Throughput
Processor time
A parser processes 6000 records in a measured batch time of 0.936 s. A separately measured benchmark harness contributes 0.036 s per batch. Assume this overhead is additive and unchanged when parsing occurs. Estimate the average parsing time per record, excluding harness overhead, in microseconds to one decimal place.