double operator() ();
private:
-
+
+ /**
+ * Value of the user time when #start#
+ * was called the last time or when the
+ * object was created and no #stop# was
+ * issued in between.
+ */
double start_time;
+
+ /**
+ * Accumulated time for all previous
+ * #start#/#stop# cycles. The time for
+ * the present cycle is not included.
+ */
double cumulative_time;
- static const double overtime;
+
+ /**
+ * Number of times that the counter
+ * had an overflow. We need to adjust the
+ * total time by this number times the
+ * number of seconds after which an
+ * overflow occurs.
+ */
unsigned int overflow;
/**
* running.
*/
bool running;
-
+
+ /**
+ * Return #cumulative_time# plus the
+ * number of overflows times the number
+ * of seconds per overflow. Do not include
+ * the time since the last overflow
+ * occured.
+ */
double full_time() const;
};
#include <sys/time.h>
-#ifndef TIMER
-#define TIMER static_cast<double>(clock())
-#endif
-#ifndef DIVIDE
-#define DIVIDE CLOCKS_PER_SEC
-#endif
-#ifndef OVER_TIME
-#define OVER_TIME 4294967296./DIVIDE
-#endif
+// maybe use times() instead of clock()?
+const double overtime = 4294967296./CLOCKS_PER_SEC;
-const double Timer::overtime = OVER_TIME;
Timer::Timer()
void Timer::start () {
running = true;
overflow = 0;
- start_time = TIMER / DIVIDE;
+ start_time = static_cast<double>(clock()) /
+ CLOCKS_PER_SEC;
};
double Timer::stop () {
running = false;
- double dtime = TIMER / DIVIDE - start_time;
+ double dtime = (static_cast<double>(clock()) / CLOCKS_PER_SEC -
+ start_time);
if (dtime < 0) {
overflow++;
};
double Timer::operator() () {
- if (running) {
- const double dtime = TIMER / DIVIDE - start_time;
- if (dtime < 0) {
- overflow++;
- };
-
- return dtime + full_time();
- };
-
- return full_time();
+ if (running)
+ {
+ const double dtime = static_cast<double>(clock()) / CLOCKS_PER_SEC - start_time;
+ if (dtime < 0)
+ overflow++;
+
+ return dtime + full_time();
+ }
+ else
+ return full_time();
};