-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTimer.cpp
More file actions
135 lines (114 loc) · 4.26 KB
/
Timer.cpp
File metadata and controls
135 lines (114 loc) · 4.26 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
///
/// Copyright (C) 2012 - All Rights Reserved
/// All rights reserved. http://www.equals-forty-two.com
///
/// @brief A Timer implementation
///
#include "Timer.h"
namespace eh
{
Timer::Timer() : mRunning(false), mAccumulatedTime(0)
{
QueryPerformanceFrequency(&mFrequency);
}
//===========================================================================
// Init a timer. If it is already running, let it continue running.
// Print an optional message.
void Timer::reset()
{
mAccumulatedTime = 0;
// Set timer status to not running
mRunning = false;
}
//===========================================================================
// Start a timer. If it is already running, let it continue running.
// Print an optional message.
void Timer::start()
{
// Return immediately if the timer is already running
if (mRunning)
return;
// Set timer status to running and set the start time
mRunning = true;
QueryPerformanceCounter(&mStartTime);
}
//===========================================================================
// Turn the timer off and start it again from 0. Print an optional message.
void Timer::restart()
{
// Set timer status to running, reset accumulated time, and set start time
mRunning = true;
mAccumulatedTime = 0;
QueryPerformanceCounter(&mStartTime);
}
//===========================================================================
// Stop the timer and print an optional message.
void Timer::stop()
{
// Compute accumulated running time and set timer status to not running
if (mRunning) {
mAccumulatedTime += getElapsedTime();
}
mRunning = false;
}
//===========================================================================
// Print out an optional message followed by the current timer timing.
void Timer::check(std::ostream& os /*= std::cout*/)
{
os << "Elapsed time [" << *this << "] seconds\n";
}
//===========================================================================
// Return the total time that the timer has been in the "running"
// state since it was first "started" or last "restarted". For
// "short" time periods (less than an hour), the actual cpu time
// used is reported instead of the elapsed time.
double Timer::getElapsedTime() const
{
LARGE_INTEGER currentTime;
QueryPerformanceCounter(¤tTime);
return (currentTime.QuadPart - mStartTime.QuadPart) / double(mFrequency.QuadPart);
}
//===========================================================================
double Timer::getTime() const
{
if (mRunning) {
return mAccumulatedTime + getElapsedTime();
}
// not running
return mAccumulatedTime;
}
//===========================================================================
// Allow timers to be printed to ostreams using the syntax 'os << t'
// for an ostream 'os' and a timer 't'. For example, "cout << t" will
// print out the total amount of time 't' has been "running".
inline std::ostream& operator<<(std::ostream& os, const Timer& t)
{
os << std::setprecision(2) << std::setiosflags(std::ios::fixed) << t.mAccumulatedTime + (t.isRunning() ? t.getElapsedTime() : 0);
return os;
}
//===========================================================================
DelayTimer::DelayTimer()
: mDelay(0)
{
}
//===========================================================================
DelayTimer::DelayTimer(double delay)
: mDelay(delay)
{
set(delay);
}
//===========================================================================
void DelayTimer::set(double delay)
{
mDelay = delay;
restart();
}
//===========================================================================
bool DelayTimer::check() const
{
if (getTime() >= mDelay) {
return true;
}
return false;
}
}