Simpatico  v1.10
Timer.h
1 #ifndef UTIL_TIMER_H
2 #define UTIL_TIMER_H
3 
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <util/global.h>
12 
13 #include <ctime>
14 
15 namespace Util
16 {
17 
30  class Timer
31  {
32 
33  public:
34 
36  Timer();
37 
39  void start();
40 
42  void stop();
43 
45  void clear();
46 
48  double time();
49 
51  bool isRunning();
52 
53  private:
54 
56  double time_;
57 
59  double begin_;
60 
62  bool isRunning_;
63 
64  };
65 
69  inline Timer::Timer()
70  : time_(0.0),
71  begin_(0.0),
72  isRunning_(false)
73  {}
74 
75  /*
76  * Start the timer.
77  */
78  inline void Timer::start()
79  {
80  if (isRunning_)
81  UTIL_THROW("Attempt to restart an active Timer");
82  isRunning_ = true;
83  begin_ = clock();
84  }
85 
86  /*
87  * Stop the timer.
88  */
89  inline void Timer::stop()
90  {
91  double end = clock();
92  if (!isRunning_)
93  UTIL_THROW("Attempt to stop an inactive Timer");
94  isRunning_ = false;
95  time_ += double(end - begin_)/double(CLOCKS_PER_SEC);
96  }
97 
98  /*
99  * Clear the timer.
100  */
101  inline void Timer::clear()
102  {
103  time_ = 0.0;
104  isRunning_ = false;
105  }
106 
107  /*
108  * Get the current time.
109  */
110  inline double Timer::time()
111  { return time_; }
112 
113  /*
114  * Is this timer running?
115  */
116  inline bool Timer::isRunning()
117  { return isRunning_; }
118 
119 }
120 #endif
File containing preprocessor macros for error handling.
double time()
Get the accumulated time, in seconds.
Definition: Timer.h:110
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void stop()
Stop the clock, increment time.
Definition: Timer.h:89
Utility classes for scientific computation.
Definition: accumulators.mod:1
Timer()
Constructor.
Definition: Timer.h:69
void clear()
Set accumulated time to zero.
Definition: Timer.h:101
Wall clock timer.
Definition: Timer.h:30
bool isRunning()
Is the timer running?
Definition: Timer.h:116
void start()
Start the clock.
Definition: Timer.h:78