liveBook · Manning

Timespec_get

As explained in Modern C

timespec_get is a C standard library function that obtains a calendar time with a struct timespec representation for a specified time base.

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%@!

Calls with TIME_UTC record times around three loops. Differences between consecutive readings can be used to examine the loops’ running times. section 15.3

timespec_get obtains a time in a struct timespec for a specified time base. Its tv_nsec member represents the subsecond part in nanoseconds, allowing finer representation than the seconds granularity described for time_t. Nanosecond representation does not by itself guarantee nanosecond measurement accuracy. section 8.5

time_t time(time_t *t);
double difftime(time_t time1, time_t time0);
time_t mktime(struct tm tm[1]);
size_t strftime(char s[static 1], size_t max,
                char const format[static 1],
                struct tm const tm[static 1]);
int timespec_get(struct timespec ts[static 1], int base);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":22}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":11},{\"line\":1,\"ch\":15}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":11},{\"line\":3,\"ch\":15}],[{\"line\":6,\"ch\":4},{\"line\":6,\"ch\":8}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":22}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":6}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":15}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":22}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":22}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":22}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":13}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":20}],[{\"line\":5,\"ch\":16},{\"line\":5,\"ch\":22}],[{\"line\":6,\"ch\":17},{\"line\":6,\"ch\":23}],[{\"line\":2,\"ch\":21},{\"line\":2,\"ch\":26}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":6}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":15}],[{\"line\":3,\"ch\":16},{\"line\":3,\"ch\":20}],[{\"line\":4,\"ch\":16},{\"line\":4,\"ch\":20}],[{\"line\":3,\"ch\":23},{\"line\":3,\"ch\":29}],[{\"line\":4,\"ch\":34},{\"line\":4,\"ch\":40}],[{\"line\":5,\"ch\":35},{\"line\":5,\"ch\":41}],[{\"line\":6,\"ch\":36},{\"line\":6,\"ch\":42}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":6}],[{\"line\":4,\"ch\":16},{\"line\":4,\"ch\":26}],[{\"line\":3,\"ch\":23},{\"line\":3,\"ch\":29}],[{\"line\":4,\"ch\":34},{\"line\":4,\"ch\":40}],[{\"line\":5,\"ch\":35},{\"line\":5,\"ch\":41}],[{\"line\":6,\"ch\":36},{\"line\":6,\"ch\":42}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":20}],[{\"line\":5,\"ch\":16},{\"line\":5,\"ch\":22}],[{\"line\":6,\"ch\":17},{\"line\":6,\"ch\":23}],[{\"line\":2,\"ch\":21},{\"line\":2,\"ch\":23}],[{\"line\":5,\"ch\":23},{\"line\":5,\"ch\":25}],[{\"line\":4,\"ch\":21},{\"line\":4,\"ch\":26}],[{\"line\":5,\"ch\":26},{\"line\":5,\"ch\":31}],[{\"line\":2,\"ch\":21},{\"line\":2,\"ch\":23}],[{\"line\":5,\"ch\":23},{\"line\":5,\"ch\":25}],[{\"line\":3,\"ch\":23},{\"line\":3,\"ch\":29}],[{\"line\":4,\"ch\":34},{\"line\":4,\"ch\":40}],[{\"line\":5,\"ch\":35},{\"line\":5,\"ch\":41}],[{\"line\":6,\"ch\":36},{\"line\":6,\"ch\":42}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":3}],[{\"line\":6,\"ch\":4},{\"line\":6,\"ch\":16}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":20}],[{\"line\":5,\"ch\":16},{\"line\":5,\"ch\":22}],[{\"line\":6,\"ch\":17},{\"line\":6,\"ch\":23}],[{\"line\":6,\"ch\":4},{\"line\":6,\"ch\":12}],[{\"line\":3,\"ch\":23},{\"line\":3,\"ch\":29}],[{\"line\":4,\"ch\":34},{\"line\":4,\"ch\":40}],[{\"line\":5,\"ch\":35},{\"line\":5,\"ch\":41}],[{\"line\":6,\"ch\":36},{\"line\":6,\"ch\":42}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":3}]]"}
!@%STYLE%@!

The declaration takes a struct timespec destination and an integer time base, and returns an int. It appears alongside other time interfaces. section 8.5

TIME_UTC is the only base value defined by the C standard in this interface. A reading with that base is expected to be consistent with time: both refer to Earth’s reference time. Platforms may offer additional bases, such as clocks relative to system startup or processor use; do not assume those bases are portable. section 8.5

TAKEAWAY: Timing changes the program being timed. A timespec_get call can inhibit compiler optimizations and may affect scheduling or caches. Assess the measurement’s own bias, repeat experiments, and collect statistics such as the mean and standard deviation. section 15.3

How Modern C covers it

How deep it goes
Explains the declaration, time base and precision, then examines measurement bias and repeated timing; it does not detail return-value semantics.
Examples
Times successive loops with TIME_UTC, including loops using volatile variables and a loop calling timespec_get itself.
What you can do after
Apply timespec_get to instrument code and assess timing bias through repeated measurements and statistics.
How it connects
Connects struct timespec and TIME_UTC with time_t, monotonic clocks, processor time, and instrumentation bias.

Also explained in

Compare how 2 books explain timespec_get →

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.