When seconds are too coarse, `timespec_get` obtains a time value in a `struct timespec`. The structure has a `tv_nsec` member for nanosecond precision, whereas `time_t` and `time` have a granularity of seconds. Nanosecond precision does not, by itself, establish the resolution of the clock. section 8.6
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);
int timespec_getres(struct timespec ts[static 1], int base);
The declaration is `int timespec_get(struct timespec ts[static 1], int base);`: supply storage for the result and choose a time base. `TIME_UTC` is the only base the C standard requires; calls using it should be consistent with `time`. Since C23, `timespec_getres` can query the clock’s resolution. section 8.6
Bracket a computation
A timing experiment calls `timespec_get(&t[0], TIME_UTC);`, runs a loop, then calls `timespec_get(&t[1], TIME_UTC);`. The two stored values mark the interval to be measured. Repeating the experiment and collecting statistics helps assess variation rather than relying on one timing. section 16.4
TAKEAWAY: A clock read is part of the experiment. Calling `timespec_get` takes time and can change optimization opportunities and runtime behavior, including scheduling and caches. Measure that bias before drawing performance conclusions. `clock` serves a different purpose—processing time attributed to the current execution—while C23 also adds optional bases for active or monotonic time. section 16.4 section 8.6
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 separates loop costs from added instrumentation. In this experiment, the loop with `timespec_get` costs substantially more per iteration than the preceding loop without it; the figures describe this measurement, not a universal call cost. section 16.4

