5 #include <sys/resource.h>
9 void timerclear(struct timeval& a)
16 struct timeval operator+(const struct timeval& a, const struct timeval& b)
18 struct timeval result;
19 result.tv_sec = a.tv_sec + b.tv_sec;
20 result.tv_usec = a.tv_usec + b.tv_usec;
21 if (result.tv_usec >= 1000000) {
23 result.tv_usec -= 1000000;
29 struct timeval operator-(const struct timeval& a, const struct timeval& b)
31 struct timeval result;
32 result.tv_sec = a.tv_sec - b.tv_sec;
33 result.tv_usec = a.tv_usec - b.tv_usec;
34 if (result.tv_usec < 0) {
36 result.tv_usec += 1000000;
41 double timertod(const struct timeval& a)
43 return a.tv_sec + a.tv_usec / 1e6;
50 struct timeval difference;
60 timerclear(&before.ru_utime);
61 timerclear(&before.ru_stime);
62 timerclear(&after.ru_utime);
63 timerclear(&after.ru_stime);
64 timerclear(&difference);
69 getrusage(RUSAGE_SELF, &before);
74 getrusage(RUSAGE_SELF, &after);
75 difference = difference + ((after.ru_utime + after.ru_stime) -
76 (before.ru_utime + before.ru_stime));
79 struct timeval tv_duration() const
84 double duration() const
86 return timertod(difference);