Evo C++ Library v0.5.1
timer.h
Go to the documentation of this file.
1 // Evo C++ Library
2 /* Copyright 2019 Justin Crowell
3 Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
4 */
6 
7 #pragma once
8 #ifndef INCL_evo_timer_h
9 #define INCL_evo_timer_h
10 
11 #include "impl/systime.h"
12 
13 namespace evo {
18 
20 
24 class TimerStampWall : public SysTimestamp {
25 public:
26  void set() {
28  }
29 
30 private:
31  void set_wall_datetime() EVO_ONCPP11(= delete);
32  void set_cpu() EVO_ONCPP11(= delete);
33 };
34 
38 class TimerStampCpu : public SysTimestamp {
39 public:
40  void set() {
41  set_cpu();
42  }
43 
44 private:
45  void set_wall_datetime() EVO_ONCPP11(= delete);
46  void set_wall_timer() EVO_ONCPP11(= delete);
47 };
48 
50 
55 template<class T=TimerStampWall>
56 class TimerT {
57 public:
58  typedef TimerT<T> This;
59 
61  TimerT() {
62  memset(this, 0, sizeof(This));
63  }
64 
68  TimerT(const This& src) {
69  memcpy(this, &src, sizeof(This));
70  }
71 
76  This& operator=(const This& src) {
77  memcpy(this, &src, sizeof(This));
78  return *this;
79  }
80 
82  void clear() {
83  memset(this, 0, sizeof(This));
84  }
85 
89  bool active() const {
90  return active_;
91  }
92 
98  double sec() const {
99  return (double)nsec() / SysTimestamp::NSEC_PER_SEC;
100  }
101 
107  ulongl msec() const {
108  return elapsed() / SysTimestamp::NSEC_PER_MSEC;
109  }
110 
116  ulongl usec() const {
117  return elapsed() / SysTimestamp::NSEC_PER_USEC;
118  }
119 
125  ulongl nsec() const {
126  return elapsed();
127  }
128 
133  void start() {
134  elapsed_ = 0;
135  active_ = true;
136  start_.set();
137  }
138 
144  void resume() {
145  active_ = true;
146  start_.set();
147  }
148 
153  This& stop() {
154  elapsed_ = elapsed();
155  active_ = false;
156  return *this;
157  }
158 
159 private:
160  T start_;
161  ulongl elapsed_;
162  bool active_;
163 
164  ulongl elapsed() const {
165  ulongl nsec = elapsed_;
166  if (active_) {
167  T end;
168  end.set();
169  nsec += end.diff_nsec(start_);
170  }
171  return nsec;
172  }
173 };
174 
176 
201 
226 
228 
229 }
230 #endif
ulongl nsec() const
Get current time elapsed in nanoseconds.
Definition: timer.h:125
Evo system time implementation helpers.
This & stop()
Stop timer.
Definition: timer.h:153
Timer that works like a stopwatch.
Definition: timer.h:56
This & operator=(const This &src)
Assignment/Copy operator.
Definition: timer.h:76
void set_wall_timer()
Set as current real (wall clock) time for use by timers.
Definition: systime.h:392
#define EVO_ONCPP11(EXPR)
Compile EXPR only if C++11 support is detected, otherwise this is a no-op.
Definition: sys.h:259
TimerT()
Constructor.
Definition: timer.h:61
Holds a CPU (process) timer-stamp, used as template argument with TimerT.
Definition: timer.h:38
ulongl msec() const
Get current time elapsed in milliseconds.
Definition: timer.h:107
Holds a real (wall clock) timer-stamp, used as template argument with TimerT.
Definition: timer.h:24
void resume()
Resume timer from last stop.
Definition: timer.h:144
TimerT(const This &src)
Copy constructor.
Definition: timer.h:68
TimerT< T > This
This type.
Definition: timer.h:58
Evo C++ Library namespace.
Definition: alg.h:11
double sec() const
Get current time elapsed in floating-point seconds (with fraction).
Definition: timer.h:98
ulongl usec() const
Get current time elapsed in microseconds.
Definition: timer.h:116
TimerT< TimerStampCpu > TimerCpu
Timer that works like a stopwatch and measures CPU (process) time.
Definition: timer.h:225
static const ulong NSEC_PER_USEC
Nanoseconds per microsecond.
Definition: systime.h:274
TimerT< TimerStampWall > Timer
Timer that works like a stopwatch and measures real (wall clock) time.
Definition: timer.h:200
void clear()
Stop and clear timer.
Definition: timer.h:82
bool active() const
Get whether timer is active (started).
Definition: timer.h:89
Holds a system timestamp for storing date/time and measuring elapsed time.
Definition: systime.h:269
static const ulong NSEC_PER_SEC
Nanoseconds per second.
Definition: systime.h:270
void start()
Start timer.
Definition: timer.h:133
static const ulong NSEC_PER_MSEC
Nanoseconds per millisecond.
Definition: systime.h:273
ulong nsec
Number of nanoseconds from seconds.
Definition: systime.h:284