liveBook · Manning

Measurement

As explained in Modern C, Third Edition

A measurement is an observed quantitative value obtained from a program execution or other empirical process.

A measurement is an observed quantitative value, such as the elapsed time of a program execution. Performance claims need measurements and statistics; algorithm selection also requires a separate complexity assessment supported by proof. section 16.4 section 16.5

Listing 16.1 Measuring several code snippets repeatedly
    timespec_get(&t[0], TIME_UTC);
    /* Volatile for i ensures that the loop is effected */
    for (uint64_t volatile i = 0; i < iterations; ++i) {
      /* do nothing */
    }
    timespec_get(&t[1], TIME_UTC);
    /* s must be volatile to ensure that the loop is effected */
    for (uint64_t i = 0; i < iterations; ++i) {
      s = i;
    }
    timespec_get(&t[2], TIME_UTC);
    /* Opaque computation ensures that the loop is effected */
    for (uint64_t i = 1; accu0 < upper; i += 2) {
      accu0 += i;
    }
    timespec_get(&t[3], TIME_UTC);
    

These C loops are bracketed by calls to timespec_get. Volatile accesses and an opaque computation help ensure that the loops are effected. section 16.4

The timing calls are part of the measured execution. They consume time and can also suppress compiler optimizations: a call may require registers to be saved and assumptions about execution state to be dropped. A system call can affect scheduling or invalidate data caches. TAKEAWAY: All measurements introduce bias. section 16.4

Repeat measurements and summarize their variation before judging a difference. For a loop, dividing mean elapsed time by the iteration count gives a per-iteration figure, but the measurement and statistics collection themselves have costs. When done correctly, their overhead can be negligible. section 16.4 section 16.5

Table 16.1 Comparison of measurements
Loop Sec per iteration Difference Gain/loss Conclusive
0 volatile loop 1.97⋅10-09
1 volatile store 3.66⋅10-10 -1.60⋅10-09 -81% Yes
2 Opaque addition 3.45⋅10-10 -2.10⋅10-11 -6% No
3 Plus timespec_get 1.77⋅10-08 1.74⋅10-08 +5043% Yes
4 Plus mean 2.62⋅10-08 8.5⋅10-09 +48% Yes
5 Plus variance 2.90⋅10-08 2.8⋅10-09 +11% Yes
6 Plus skew 3.17⋅10-08 2.7⋅10-09 +9% Yes

The comparison reports a conclusive difference between two volatile loops, but not the smaller difference between a volatile store and an opaque addition. It also shows the added cost of timing and statistics collection. section 16.4

Inspect generated code when interpreting timings: source loops that look similar can compile into different instruction sequences. Do not trade correctness for performance, and do not treat an apparent speedup as established without thorough measurements and statistics. section 16.4 section 16.5

How Modern C, Third Edition covers it

How deep it goes
Works through repeated timing, statistical summaries, overhead and generated code, while deferring a full treatment of measurement science and complexity proofs.
Examples
Compares timed C loops, calls to timespec_get, and successive levels of statistics collection using per-iteration results and assembler listings.
What you can do after
Evaluate performance differences using repeated timings and statistics while recognizing instrumentation bias and inconclusive comparisons.
How it connects
Connects measurement with statistics, compiler optimization, algorithmic complexity and correctness.

Also explained in

Compare how 4 books explain measurement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.