Attachment 'Timer.h'
Download 1 #ifndef __TIMER_H__
2 #define __TIMER_H__
3
4 #include <sys/time.h>
5
6 //! A SIMPLE CLASS FOR TIME MEASUREMENTS.
7 //
8 class Timer
9 {
10 private:
11
12 //! The timer value (initially zero)
13 off_t _usecs;
14
15 //! The timer value at the last mark set (initially zero)
16 off_t _usecs_at_mark;
17
18 //! Used by the gettimeofday command.
19 struct timeval _tstart;
20
21 //! Used by the gettimeofday command.
22 struct timeval _tend;
23
24 //! Used by the gettimeofday command.
25 struct timezone _tz;
26
27 //! Indicates whether a measurement is running.
28 bool _running;
29
30 public:
31
32 //! The default constructor.
33 Timer() { reset(); }
34
35 //! Resets the timer value to zero and stops the measurement.
36 void reset() { _usecs = _usecs_at_mark = 0; _running = false; }
37
38 //! Mark the current point in time (to be considered by next usecs_since_mark)
39 void mark() { stop(); _usecs_at_mark = _usecs; cont(); }
40
41 //! Resets the timer value to zero and starts the measurement.
42 inline void start()
43 {
44 _usecs = _usecs_at_mark = 0;
45 gettimeofday(&_tstart, &_tz);
46 _running = true;
47 }
48
49 //! Continues the measurement without resetting the timer value (no effect if running)
50 inline void cont()
51 {
52 if (_running == false)
53 {
54 gettimeofday(&_tstart, &_tz);
55 _running = true;
56 }
57 }
58
59 //! Stops the measurement (does *not* return the timer value anymore)
60 inline void stop()
61 {
62 gettimeofday(&_tend, &_tz);
63 if(_running){_usecs += (off_t)(1000000) * (off_t)(_tend.tv_sec - _tstart.tv_sec) + (off_t)(_tend.tv_usec - _tstart.tv_usec);}
64 _running = false;
65 //return _usecs;
66 }
67
68 //! Time at last stop (initially zero)
69 off_t value() const { return _usecs; } /* in microseconds */
70 off_t usecs() const { return _usecs; } /* in microseconds */
71 off_t msecs() const { return _usecs/1000; } /* in milliseconds */
72 float secs() const { return _usecs/1000000.0; } /* in seconds */
73
74 //! Time from last mark to last stop (initally zero)
75 off_t usecs_since_mark() const { return _usecs - _usecs_at_mark; }
76 };
77
78 #endif
79
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.