-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.h
More file actions
65 lines (45 loc) · 2 KB
/
Timer.h
File metadata and controls
65 lines (45 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef TIMER_GUARDIAN
#define TIMER_GUARDIAN
#endif
#if defined WIN32
#include <windows.h>
#include <stdexcept>
class Timer
{
public:
explicit Timer (bool started = false) : m_startTicks(-1), m_totalTicks(0) { if (started) start(); }
Timer (const Timer& other) { operator=(other); }
~Timer (void) {}
Timer& operator= (const Timer& other) { m_startTicks = other.m_startTicks; m_totalTicks = other.m_totalTicks; return *this; }
void start (void) { m_startTicks = queryTicks(); }
float getElapsed (void) { return ticksToSecs(getElapsedTicks()); }
private:
void unstart(void) { m_startTicks = -1; }
float end (void); // return elapsed, total += elapsed, restart
float getTotal (void) const { return ticksToSecs(m_totalTicks); }
void clearTotal (void) { m_totalTicks = 0; }
private:
static long long int queryTicks (void);
static float ticksToSecs (long long int ticks);
long long int getElapsedTicks (void); // return time since start, start if unstarted
private:
static double s_ticksToSecsCoef;
static long long int s_prevTicks;
long long int m_startTicks;
long long int m_totalTicks;
};
#else
#include <sys/time.h> // for gettimeofday
class Timer
{
public:
explicit Timer (bool started = false) { m_timeVal = {0,0}; if (started) start(); }
Timer (const Timer& other) = default;
~Timer () = default;
Timer& operator= (const Timer& other) = default;
void start () { gettimeofday(&m_timeVal, nullptr);}
float getElapsed ();
private:
timeval m_timeVal;
};
#endif