NEST  2.6.0,not_revisioned_source_dir@0
stopwatch.h
Go to the documentation of this file.
1 /*
2  * stopwatch.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef STOPWATCH_H
24 #define STOPWATCH_H
25 
26 #include <iostream>
27 #include <sys/time.h>
28 #include <cassert>
29 
30 namespace nest
31 {
32 
33 /***********************************************************************
34  * Stopwatch *
35  * Accumulates time between start and stop, and provides *
36  * the elapsed time with different time units. *
37  * *
38  * Partly inspired by com.google.common.base.Stopwatch.java *
39  * Not thread-safe: - Do not share stopwatches among threads. *
40  * - Let each thread have its own stopwatch. *
41  * *
42  * Usage example: *
43  * Stopwatch x; *
44  * x.start(); *
45  * // ... do computations for 15.34 sec *
46  * x.stop(); // only pauses stopwatch *
47  * x.print("Time needed "); // > Time needed 15.34 sec. *
48  * x.start(); // resumes stopwatch *
49  * // ... next computations for 11.22 sec *
50  * x.stop(); *
51  * x.print("Time needed "); // > Time needed 26,56 sec. *
52  * x.reset(); // reset to default values *
53  * x.start(); // starts the stopwatch from 0 *
54  * // ... computation 5.7 sec *
55  * x.print("Time "); // > Time 5.7 sec. *
56  * // ^ intermediate timing without stopping the stopwatch *
57  * // ... more computations 1.7643 min *
58  * x.stop(); *
59  * x.print("Time needed ", Stopwatch::MINUTES, std::cerr); *
60  * // > Time needed 1,8593 min. (on cerr) *
61  * // other units and output streams possible *
62  ***********************************************************************/
63 class Stopwatch
64 {
65 public:
66  typedef size_t timestamp_t;
67  typedef size_t timeunit_t;
68 
69  enum
70  {
72  MILLISEC = MICROSEC * 1000,
73  SECONDS = MILLISEC * 1000,
74  MINUTES = SECONDS * 60,
75  HOURS = MINUTES * 60,
76  DAYS = HOURS * 24
77  };
78 
79  static bool correct_timeunit(timeunit_t t);
80 
84  Stopwatch(){ reset(); }
85 
89  void start();
90 
94  void stop();
95 
99  bool isRunning() const;
100 
109  double elapsed(timeunit_t timeunit = SECONDS) const;
110 
122 
126  void reset();
127 
131  void print(const char* msg = "", timeunit_t timeunit = SECONDS,
132  std::ostream& os = std::cout) const;
133 
138  friend std::ostream& operator<< (std::ostream& os,
139  const Stopwatch& stopwatch);
140 
141 private:
142 #ifndef DISABLE_TIMING
145  bool _running;
146 #endif
147 
151  static timestamp_t get_timestamp();
152 };
153 
154 inline
155 bool
157 {
158  return t == MICROSEC || t == MILLISEC || t == SECONDS ||
159  t == MINUTES || t == HOURS || t == DAYS;
160 }
161 
162 inline
163 void
165 {
166 #ifndef DISABLE_TIMING
167  if(!isRunning())
168  {
169  _prev_elapsed += _end - _beg; // store prev. time, if we resume
170  _end = _beg = get_timestamp(); // invariant: _end >= _beg
171  _running = true; // we start running
172  }
173 #endif
174 }
175 
176 inline
177 void
179 {
180 #ifndef DISABLE_TIMING
181  if(isRunning())
182  {
183  _end = get_timestamp(); // invariant: _end >= _beg
184  _running = false; // we stopped running
185  }
186 #endif
187 }
188 
189 inline
190 bool
192 {
193 #ifndef DISABLE_TIMING
194  return _running;
195 #else
196  return false;
197 #endif
198 }
199 
200 inline
201 double
203 {
204 #ifndef DISABLE_TIMING
205  assert(correct_timeunit(timeunit));
206  return 1.0 * elapsed_timestamp() / timeunit;
207 #else
208  return 0.0;
209 #endif
210 }
211 
212 inline
215 {
216 #ifndef DISABLE_TIMING
217  if(isRunning())
218  {
219  // get intermediate elapsed time; do not change _end, to be const
220  return get_timestamp() - _beg + _prev_elapsed;
221  }
222  else
223  {
224  // stopped before, get time of current measurment + last measurments
225  return _end - _beg + _prev_elapsed;
226  }
227 #else
228  return (timestamp_t)0;
229 #endif
230 }
231 
232 inline
233 void
235 {
236 #ifndef DISABLE_TIMING
237  _beg = 0; // invariant: _end >= _beg
238  _end = 0;
239  _prev_elapsed = 0; // erase all prev. measurments
240  _running = false; // of course not running.
241 #endif
242 }
243 
244 inline
245 void
246 nest::Stopwatch::print(const char* msg, timeunit_t timeunit,
247  std::ostream& os) const
248 {
249 #ifndef DISABLE_TIMING
250  assert(correct_timeunit(timeunit));
251  double e = elapsed(timeunit);
252  os << msg << e;
253  switch (timeunit)
254  {
255  case MICROSEC: os << " microsec."; break;
256  case MILLISEC: os << " millisec."; break;
257  case SECONDS: os << " sec."; break;
258  case MINUTES: os << " min."; break;
259  case HOURS: os << " h."; break;
260  case DAYS: os << " days."; break;
261  }
262 #ifdef DEBUG
263  os << " (running: " << (_running ? "true" : "false")
264  << ", begin: " << _beg
265  << ", end: " << _end
266  << ", diff: " << (_end - _beg)
267  << ", prev: " << _prev_elapsed << ")";
268 #endif
269  os << std::endl;
270 #endif
271 }
272 
273 inline
276 {
277  // works with:
278  // * hambach (Linux 2.6.32 x86_64)
279  // * JuQueen (BG/Q)
280  // * MacOS 10.9
281  struct timeval now;
282  gettimeofday (&now, (struct timezone*)0);
283  return (nest::Stopwatch::timestamp_t) now.tv_usec +
285 }
286 
287 } /* namespace timer */
288 #endif /* STOPWATCH_H */
size_t timeunit_t
Definition: stopwatch.h:67
void start()
Starts or resumes the stopwatch, if it is not running already.
Definition: stopwatch.h:164
timestamp_t _end
Definition: stopwatch.h:143
Definition: stopwatch.h:74
friend std::ostream & operator<<(std::ostream &os, const Stopwatch &stopwatch)
Convenient method for writing time in seconds to some ostream.
Definition: stopwatch.cpp:27
Definition: stopwatch.h:75
timestamp_t elapsed_timestamp() const
Returns the time elapsed between the start and stop of the stopwatch.
Definition: stopwatch.h:214
static bool correct_timeunit(timeunit_t t)
Definition: stopwatch.h:156
Definition: stopwatch.h:63
assert(pNet!=0)
void reset()
Resets the stopwatch.
Definition: stopwatch.h:234
static timestamp_t get_timestamp()
Returns current time in microseconds since EPOCH.
Definition: stopwatch.h:275
void print(const char *msg="", timeunit_t timeunit=SECONDS, std::ostream &os=std::cout) const
This method prints out the currently elapsed time.
Definition: stopwatch.h:246
timestamp_t _beg
Definition: stopwatch.h:143
Definition: stopwatch.h:76
Definition: stopwatch.h:72
bool _running
Definition: stopwatch.h:145
Definition: stopwatch.h:73
Stopwatch()
Creates a stopwatch that is not running.
Definition: stopwatch.h:84
void stop()
Stops the stopwatch, if it is not stopped already.
Definition: stopwatch.h:178
size_t _prev_elapsed
Definition: stopwatch.h:144
bool isRunning() const
Returns, whether the stopwatch is running.
Definition: stopwatch.h:191
const double e
Definition: numerics.cpp:62
Definition: stopwatch.h:71
double elapsed(timeunit_t timeunit=SECONDS) const
Returns the time elapsed between the start and stop of the stopwatch.
Definition: stopwatch.h:202
size_t timestamp_t
Definition: stopwatch.h:66