7 struct timespec operator+(const struct timespec& a, const struct timespec& b)
9 struct timespec result;
10 result.tv_sec = a.tv_sec + b.tv_sec;
11 result.tv_nsec = a.tv_nsec + b.tv_nsec;
12 if (result.tv_nsec >= 1000000000L) {
14 result.tv_nsec -= 1000000000L;
20 struct timespec operator-(const struct timespec& a, const struct timespec& b)
22 struct timespec result;
23 result.tv_sec = a.tv_sec - b.tv_sec;
24 result.tv_nsec = a.tv_nsec - b.tv_nsec;
25 if (result.tv_nsec < 0) {
27 result.tv_nsec += 1000000000L;
34 enum clock_type { wallclock_time, cpu_time };
36 timestamp(clock_type ct);
43 struct timespec tv_duration() const;
44 double duration() const;
48 struct timespec before;
49 struct timespec after;
50 struct timespec difference;
52 void get_time(struct timespec& tv);
54 static void tv_clear(struct timespec& a);
55 static double timertod(const struct timespec& a);
59 timestamp::timestamp(clock_type ct): clk(ct)
65 timestamp::~timestamp()
70 void timestamp::reset()
78 void timestamp::start()
84 void timestamp::stop()
87 difference = difference + (after - before);
91 struct timespec timestamp::tv_duration() const
97 double timestamp::duration() const
99 return timertod(difference);
103 void timestamp::get_time(struct timespec& tv)
107 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tv);
111 #ifdef CLOCK_MONOTONIC_RAW
112 clock_gettime(CLOCK_MONOTONIC_RAW, &tv);
114 clock_gettime(CLOCK_MONOTONIC, &tv);
121 void timestamp::tv_clear(struct timespec& a)
128 double timestamp::timertod(const struct timespec& a)
130 return a.tv_sec + a.tv_nsec / 1e9;