liveBook · Manning

Statistical sample

As explained in Modern C

A statistical sample is a finite collection of observations drawn from a phenomenon or population.

Run a timed code snippet repeatedly. Each elapsed time is an observation; the collected times form a statistical sample. A frequency histogram shows how often measured values occur. TAKEAWAY: assess performance from repeated measurements, not an isolated timing. section 15.3

Listing 15.2. Collecting time statistics with timespec_diff and stats_collect2
102      for (unsigned i = 0; i < loops; i++) {
103        double diff = timespec_diff(&t[i+1], &t[i]);
104         stats_collect2(&statistic[i], diff);
105      }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":3}],[{\"line\":0,\"ch\":9},{\"line\":0,\"ch\":12}],[{\"line\":0,\"ch\":14},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":3}],[{\"line\":1,\"ch\":11},{\"line\":1,\"ch\":17}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":3}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":3}]]"}
!@%STYLE%@!

The loop computes each elapsed time with timespec_diff and adds it to a running statistic with stats_collect2. section 15.3

The mean summarizes the measured times, while relative standard deviation reports their variation. Neither removes measurement bias: calling timespec_get takes time and can change optimization opportunities, scheduling, or cache behavior. Keep that disturbance small enough to assess the result qualitatively. section 15.3

How Modern C covers it

How deep it goes
Shows timing collection and summary statistics, but does not develop experimental measurement in full detail.
Examples
Uses repeated loop timings, a frequency histogram, and printed means with relative standard deviations.
What you can do after
Apply it via timespec_diff and stats_collect2 to collect elapsed-time observations and assess their summaries cautiously.
How it connects
Connects samples to frequency histograms, means, relative standard deviation, and measurement bias.

Also explained in

Compare how 7 books explain statistical sample →

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.