5 #include <sys/resource.h>
10 # define HAVE_TIMERCLEAR
12 # warning _BSD_SOURCE not defined
15 # undef HAVE_TIMERCLEAR
19 struct timeval operator+(const struct timeval& a, const struct timeval& b)
21 struct timeval result;
23 timeradd(&a, &b, &result);
25 result.tv_sec = a.tv_sec + b.tv_sec;
26 result.tv_usec = a.tv_usec + b.tv_usec;
27 if (result.tv_usec >= 1000000) {
29 result.tv_usec -= 1000000;
36 struct timeval operator-(const struct timeval& a, const struct timeval& b)
38 struct timeval result;
40 timersub(&a, &b, &result);
42 result.tv_sec = a.tv_sec - b.tv_sec;
43 result.tv_usec = a.tv_usec - b.tv_usec;
44 if (result.tv_usec < 0) {
46 result.tv_usec += 1000000;
61 struct timeval tv_duration() const;
62 double duration() const;
67 struct timeval difference;
69 static void tv_clear(struct timeval& a);
70 static double timertod(const struct timeval& a);
74 timestamp::timestamp()
80 timestamp::~timestamp()
85 void timestamp::reset()
87 tv_clear(before.ru_utime);
88 tv_clear(before.ru_stime);
89 tv_clear(after.ru_utime);
90 tv_clear(after.ru_stime);
95 void timestamp::start()
97 getrusage(RUSAGE_SELF, &before);
101 void timestamp::stop()
103 getrusage(RUSAGE_SELF, &after);
104 difference = difference + ((after.ru_utime + after.ru_stime) -
105 (before.ru_utime + before.ru_stime));
109 struct timeval timestamp::tv_duration() const
115 double timestamp::duration() const
117 return timertod(difference);
121 void timestamp::tv_clear(struct timeval& a)
123 #ifdef HAVE_TIMERCLEAR
126 tv.sec = tv.usec = 0;
131 double timestamp::timertod(const struct timeval& a)
133 return a.tv_sec + a.tv_usec / 1e6;