liveBook · Manning

Measurement

As explained in Modern C

Measurement is the systematic assessment of a property, behavior, or outcome that produces an observed value, score, or judgment.

Measurement assesses an observed property. For code performance, timing measurements provide evidence about execution rather than relying on speculation. They complement algorithmic complexity assessment, which requires proofs rather than timing alone. section 15.3

Listing 15.6. Instrumenting three for loops with struct timespec
53      timespec_get(&t[0], TIME_UTC);
54      /* Volatile for i ensures that the loop is effected */
55      for (uint64_t volatile i = 0; i < iterations; ++i) {
56        /* do nothing */
57      }
58      timespec_get(&t[1], TIME_UTC);
59      /* s must be volatile to ensure that the loop is effected */
60      for (uint64_t i = 0; i < iterations; ++i) {
61        s = i;
62      }
63      timespec_get(&t[2], TIME_UTC);
64      /* Opaque computation ensures that the loop is effected */
65      for (uint64_t i = 1; accu0 < upper; i += 2) {
66        accu0 += i;
67      }
68      timespec_get(&t[3], TIME_UTC);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":8},{\"line\":0,\"ch\":20}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":20}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":20}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":20}],[{\"line\":0,\"ch\":28},{\"line\":0,\"ch\":36}],[{\"line\":5,\"ch\":28},{\"line\":5,\"ch\":36}],[{\"line\":10,\"ch\":28},{\"line\":10,\"ch\":36}],[{\"line\":15,\"ch\":28},{\"line\":15,\"ch\":36}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":1,\"ch\":20},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":8},{\"line\":2,\"ch\":11}],[{\"line\":7,\"ch\":8},{\"line\":7,\"ch\":11}],[{\"line\":12,\"ch\":8},{\"line\":12,\"ch\":11}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":21}],[{\"line\":7,\"ch\":13},{\"line\":7,\"ch\":21}],[{\"line\":12,\"ch\":13},{\"line\":12,\"ch\":21}],[{\"line\":2,\"ch\":22},{\"line\":2,\"ch\":30}],[{\"line\":6,\"ch\":21},{\"line\":6,\"ch\":29}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}],[{\"line\":0,\"ch\":8},{\"line\":0,\"ch\":20}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":20}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":20}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":20}],[{\"line\":0,\"ch\":28},{\"line\":0,\"ch\":36}],[{\"line\":5,\"ch\":28},{\"line\":5,\"ch\":36}],[{\"line\":10,\"ch\":28},{\"line\":10,\"ch\":36}],[{\"line\":15,\"ch\":28},{\"line\":15,\"ch\":36}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":2}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":2}],[{\"line\":1,\"ch\":20},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":8},{\"line\":2,\"ch\":11}],[{\"line\":7,\"ch\":8},{\"line\":7,\"ch\":11}],[{\"line\":12,\"ch\":8},{\"line\":12,\"ch\":11}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":21}],[{\"line\":7,\"ch\":13},{\"line\":7,\"ch\":21}],[{\"line\":12,\"ch\":13},{\"line\":12,\"ch\":21}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":2}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":2}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":2}],[{\"line\":0,\"ch\":8},{\"line\":0,\"ch\":20}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":20}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":20}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":20}],[{\"line\":0,\"ch\":28},{\"line\":0,\"ch\":36}],[{\"line\":5,\"ch\":28},{\"line\":5,\"ch\":36}],[{\"line\":10,\"ch\":28},{\"line\":10,\"ch\":36}],[{\"line\":15,\"ch\":28},{\"line\":15,\"ch\":36}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":19}],[{\"line\":7,\"ch\":17},{\"line\":7,\"ch\":19}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}],[{\"line\":12,\"ch\":17},{\"line\":12,\"ch\":19}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":1,\"ch\":20},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":8},{\"line\":2,\"ch\":11}],[{\"line\":7,\"ch\":8},{\"line\":7,\"ch\":11}],[{\"line\":12,\"ch\":8},{\"line\":12,\"ch\":11}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":21}],[{\"line\":7,\"ch\":13},{\"line\":7,\"ch\":21}],[{\"line\":12,\"ch\":13},{\"line\":12,\"ch\":21}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":2}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":2}],[{\"line\":0,\"ch\":8},{\"line\":0,\"ch\":20}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":20}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":20}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":20}],[{\"line\":0,\"ch\":28},{\"line\":0,\"ch\":36}],[{\"line\":5,\"ch\":28},{\"line\":5,\"ch\":36}],[{\"line\":10,\"ch\":28},{\"line\":10,\"ch\":36}],[{\"line\":15,\"ch\":28},{\"line\":15,\"ch\":36}]]"}
!@%STYLE%@!

Three loops are bracketed by calls to timespec_get. The first uses a volatile loop counter, the second stores to a volatile object, and the third performs an opaque computation. section 15.3

The timestamps delimit intervals; repeated measurements can be collected as statistics. A mean describes the observed average, while relative standard deviation gives a sense of variation. Dividing the mean by the iteration count gives a per-iteration figure for comparison. section 15.3

Comparing measured loops

In one set of measurements, the volatile store averaged 3.66 × 10⁻¹⁰ seconds per iteration and the opaque addition averaged 3.45 × 10⁻¹⁰. Their reported 6% difference was not conclusive. The comparison between the volatile counter and volatile store was reported as conclusive. section 15.4

TAKEAWAY: All measurements introduce bias. A timespec_get call takes time and can also change the code being measured: the compiler must account for the call, potentially suppressing optimizations. A system call may also affect scheduling or invalidate caches. section 15.3

Inspecting generated code helps distinguish the intended operation from what actually executes. Performance claims need thorough measurements and statistics; performance must not be traded for correctness. section 15.3 section 15.4

How Modern C covers it

How deep it goes
Works through timing, statistics, instrumentation bias, and generated-code inspection while deferring a full treatment of measurement and complexity proofs.
Examples
Compares volatile loops, opaque addition, and timing calls using per-iteration measurements and conclusive or inconclusive differences.
What you can do after
Evaluate/understand performance claims using repeated timing, statistical variation, and awareness of measurement bias.
How it connects
Connects measurement to algorithmic complexity, compiler optimization, instrumentation bias, and statistics.

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. The section links point to the parts of the book it draws on.