class timestamp {
public:
- timestamp();
+ enum clock_type { wallclock_time, cpu_time };
+
+ timestamp(clock_type ct);
~timestamp();
void reset();
double duration() const;
private:
- struct rusage before;
- struct rusage after;
+ clock_type clk;
+ struct timeval before;
+ struct timeval after;
struct timeval difference;
+ void get_time(struct timeval& tv);
+
static void tv_clear(struct timeval& a);
static double timertod(const struct timeval& a);
};
inline
-timestamp::timestamp()
+timestamp::timestamp(clock_type ct): clk(ct)
{
reset();
}
inline
void timestamp::reset()
{
- tv_clear(before.ru_utime);
- tv_clear(before.ru_stime);
- tv_clear(after.ru_utime);
- tv_clear(after.ru_stime);
+ tv_clear(before);
+ tv_clear(after);
tv_clear(difference);
}
inline
void timestamp::start()
{
- getrusage(RUSAGE_SELF, &before);
+ get_time(before);
}
inline
void timestamp::stop()
{
- getrusage(RUSAGE_SELF, &after);
- difference = difference + ((after.ru_utime + after.ru_stime) -
- (before.ru_utime + before.ru_stime));
+ get_time(after);
+ difference = difference + (after - before);
}
inline
return timertod(difference);
}
+inline
+void timestamp::get_time(struct timeval& tv)
+{
+ switch (clk) {
+ case cpu_time: {
+ struct rusage usage;
+ getrusage(RUSAGE_SELF, &usage);
+ tv = usage.ru_utime + usage.ru_stime;
+ break;
+ }
+ case wallclock_time:
+ gettimeofday(&tv, NULL);
+ break;
+ }
+}
+
inline
void timestamp::tv_clear(struct timeval& a)
{