Heidelberg Educational Numerics Library  Version 0.24 (from 9 September 2011)
timer.hh
Go to the documentation of this file.
1 #ifndef DUNE_TIMER_HH
2 #define DUNE_TIMER_HH
3 
4 #ifndef TIMER_USE_STD_CLOCK
5 // headers for getrusage(2)
6 #include <sys/resource.h>
7 #endif
8 
9 #include <ctime>
10 
11 // headers for stderror(3)
12 #include <cstring>
13 
14 // access to errno in C++
15 #include <cerrno>
16 
17 #include "exceptions.hh"
18 
19 namespace hdnum {
20 
26  class TimerError : public SystemError {} ;
27 
28 
41 class Timer
42 {
43 public:
45  Timer () throw(TimerError)
46  {
47  reset();
48  }
49 
51  void reset() throw (TimerError)
52  {
53 #ifdef TIMER_USE_STD_CLOCK
54  cstart = std::clock();
55 #else
56  rusage ru;
57  if (getrusage(RUSAGE_SELF, &ru))
58  HDNUM_THROW(TimerError, strerror(errno));
59  cstart = ru.ru_utime;
60 #endif
61  }
62 
64  double elapsed () const throw (TimerError)
65  {
66 #ifdef TIMER_USE_STD_CLOCK
67  return (std::clock()-cstart) / static_cast<double>(CLOCKS_PER_SEC);
68 #else
69  rusage ru;
70  if (getrusage(RUSAGE_SELF, &ru))
71  HDNUM_THROW(TimerError, strerror(errno));
72  return 1.0 * (ru.ru_utime.tv_sec - cstart.tv_sec) + (ru.ru_utime.tv_usec - cstart.tv_usec) / (1000.0 * 1000.0);
73 #endif
74  }
75 
76 private:
77 #ifdef TIMER_USE_STD_CLOCK
78  std::clock_t cstart;
79 #else
80  struct timeval cstart;
81 #endif
82 }; // end class Timer
83 
84 } // end namespace
85 
86 #endif
#define HDNUM_THROW(E, m)
Definition: exceptions.hh:86
void reset()
Reset timer.
Definition: timer.hh:51
Default exception class for OS errors.
Definition: exceptions.hh:139
Exception thrown by the Timer class
Definition: timer.hh:26
A few common exception classes.
A simple stop watch.
Definition: timer.hh:41
Timer()
A new timer, start immediately.
Definition: timer.hh:45
Definition: densematrix.hh:21
double elapsed() const
Get elapsed user-time in seconds.
Definition: timer.hh:64