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;
54 enum clock_type { wallclock_time, cpu_time };
56 timestamp(clock_type ct);
63 struct timeval tv_duration() const;
64 double duration() const;
68 struct timeval before;
70 struct timeval difference;
72 void get_time(struct timeval& tv);
74 static void tv_clear(struct timeval& a);
75 static double timertod(const struct timeval& a);
79 timestamp::timestamp(clock_type ct): clk(ct)
85 timestamp::~timestamp()
90 void timestamp::reset()
98 void timestamp::start()
104 void timestamp::stop()
107 difference = difference + (after - before);
111 struct timeval timestamp::tv_duration() const
117 double timestamp::duration() const
119 return timertod(difference);
123 void timestamp::get_time(struct timeval& tv)
128 getrusage(RUSAGE_SELF, &usage);
129 tv = usage.ru_utime + usage.ru_stime;
133 gettimeofday(&tv, NULL);
139 void timestamp::tv_clear(struct timeval& a)
141 #ifdef HAVE_TIMERCLEAR
144 tv.sec = tv.usec = 0;
149 double timestamp::timertod(const struct timeval& a)
151 return a.tv_sec + a.tv_usec / 1e6;