HiRep 0.1
Loading...
Searching...
No Matches
timing.h
1#ifndef TIMING_H
2#define TIMING_H
3
4#include <sys/time.h>
5#include <time.h>
6#include <stdint.h>
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12// High resolution timing functions
13typedef struct timespec Instant;
14
16static inline Instant now() {
17 Instant clock;
18 clock_gettime(CLOCK_MONOTONIC_RAW, &clock);
19 return clock;
20}
22static inline double interval_usec(Instant const *end, Instant const *start) {
23 return (end->tv_sec - start->tv_sec) * 1.e6 + (end->tv_nsec - start->tv_nsec) * 1.e-3;
24}
25
27static inline uint_fast64_t interval_nsec(Instant const *end, Instant const *start) {
28 return (end->tv_sec - start->tv_sec) * (uint_fast64_t)(1000000000U) + (end->tv_nsec - start->tv_nsec);
29}
30
31typedef struct Timer {
32 Instant start;
33 Instant lap;
34} Timer;
35
37static inline void timer_set(Timer *t) {
38 Instant const n = now();
39 t->start = n;
40 t->lap = n;
41}
42
44static inline double timer_read(Timer const *t) {
45 Instant const n = now();
46 return interval_usec(&n, &(t->start));
47}
48
50static inline double timer_lap(Timer *t) {
51 Instant const l = now();
52 double laptime = interval_usec(&l, &(t->lap));
53 t->lap = l;
54 return laptime;
55}
56
58static inline double timer_res() {
59 Instant res;
60 clock_getres(CLOCK_MONOTONIC_RAW, &res);
61 return res.tv_sec * 1.e6 + res.tv_nsec * 1.e-3;
62}
63
64/* old low timing function */
65int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y);
66
67#ifdef __cplusplus
68}
69#endif
70#endif //TIMING_H
Definition timing.h:31