*
* Note: the implementation of this class is system dependant.
*
- * @author R. Becker, G. Kanschat, F.-T. Suttmeier, revised by W. Bangerth
+ * @author G. Kanschat, W. Bangerth
*/
-class Timer {
-public:
+class Timer
+{
+ public:
/**
* Constructor. Starts the timer at 0 sec.
*/
- Timer();
+ Timer ();
/**
* Re-start the timer at the point where
* it was stopped. This way a cumulative
* measurement of time is possible.
*/
- void start();
+ void start ();
/**
* Sets the current time as next
* starting time and return the
* elapsed time in seconds.
*/
- double stop();
+ double stop ();
/**
* Stop the timer if neccessary and reset
* the elapsed time to zero.
*/
- void reset();
+ void reset ();
/**
* Access to the current time
*/
double cumulative_time;
- /**
- * 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.
- */
- mutable unsigned int overflow;
/**
* Store whether the timer is presently
* the time since the last overflow
* occured.
*/
- double full_time() const;
+ double full_time () const;
};
#include <base/timer.h>
-#include <ctime>
-
-// this include should probably be properly
-// ./configure'd using the AC_HEADER_TIME macro:
-#include <sys/time.h>
-
-// maybe use times() instead of clock()?
-const double overtime = 4294967296./CLOCKS_PER_SEC;
-
+// these includes should probably be properly
+// ./configure'd using the AC_HEADER_TIME macro:
+#include <sys/resource.h>
+#include <sys/time.h>
Timer::Timer()
- : cumulative_time(0.)
+ : cumulative_time (0.)
{
start();
-};
+}
-void Timer::start () {
+void Timer::start ()
+{
running = true;
- overflow = 0;
- start_time = static_cast<double>(clock()) /
- CLOCKS_PER_SEC;
-};
+ rusage usage;
+ getrusage (RUSAGE_SELF, &usage);
+ start_time = usage.ru_utime.tv_sec + 1.e-6 * usage.ru_utime.tv_usec;
+}
-double Timer::stop () {
+double Timer::stop ()
+{
running = false;
- double dtime = (static_cast<double>(clock()) / CLOCKS_PER_SEC -
- start_time);
- if (dtime < 0) {
- overflow++;
- };
-
+ rusage usage;
+ getrusage (RUSAGE_SELF, &usage);
+ const double dtime = usage.ru_utime.tv_sec + 1.e-6 * usage.ru_utime.tv_usec;
cumulative_time += dtime;
return full_time ();
};
-double Timer::operator() () const {
+double Timer::operator() () const
+{
if (running)
{
- const double dtime = static_cast<double>(clock()) / CLOCKS_PER_SEC - start_time;
- if (dtime < 0)
- overflow++;
+ rusage usage;
+ getrusage (RUSAGE_SELF, &usage);
+ const double dtime = usage.ru_utime.tv_sec + 1.e-6 * usage.ru_utime.tv_usec;
return dtime + full_time();
}
-void Timer::reset () {
+void Timer::reset ()
+{
cumulative_time = 0.;
running = false;
};
-double Timer::full_time () const {
- return cumulative_time + overflow*overtime;
+double Timer::full_time () const
+{
+ return cumulative_time;
};