-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHRTimer.cpp
More file actions
180 lines (162 loc) · 5.18 KB
/
HRTimer.cpp
File metadata and controls
180 lines (162 loc) · 5.18 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "HRTimer.h"
namespace HRTimer
{
// Clears all current laps and takes
// a fresh timestamp.
void HiResTimer::Start()
{
mLaps.clear();
mStart = mEnd = HiResClk::now();
};
// Resets timer to start a fresh set
// of measurements
void HiResTimer::Reset()
{
Start();
};
// Stops the timer, which is to say, it
// takes and ending timestamp
void HiResTimer::Stop()
{
mEnd = std::chrono::high_resolution_clock::now();
};
// Saves a LapMarker, which is a line number and timestamp at this moment
// To function as intended, this should be called using the __LINE__ macro
// so that it will print the line number where the timepoint is measured
void HiResTimer::_Lap(int line)
{
LapMarker currentMark;
currentMark.tp = HiResClk::now();
currentMark.line = std::to_string(line);
mLaps.push_back(currentMark);
};
// Prints all the saved Lap times as a double, with line numbers
void HiResTimer::_PrintLapTimesDbl(std::string msg)
{
auto start = mStart;
for (auto lap : mLaps)
{
std::cout << std::setprecision(3) << "Func: " << msg << "Line: " << lap.line << " Lap time: " << GetDeltaTimeInDbl(start, lap.tp) << " seconds" << std::endl;
start = lap.tp;
}
}
// Prints all the saved Lap times, scaled to the appropriate seconds type, with line numbers
// To function as intended, this should be called using the __FUNC__ macro
// so that it will print the line number where the timepoint is measured
void HiResTimer::_PrintLapTimes(std::string funcName)
{
auto start = mStart;
std::string type;
for (auto lap : mLaps)
{
std::cout << std::setprecision(3) << "Func: " << funcName << "Line: " << lap.line << " - Lap time: " << GetDeltaTime(start, lap.tp, type) << type << std::endl;
start = lap.tp;
}
}
// Prints all the saved Lap times as a double, with line numbers
// To function as intended, this should be called using the __FUNC__ macro
// so that it will print the line number where the timepoint is measured
void HiResTimer::_PrintElapsedTimeDbl(std::string funcName)
{
Stop();
if (funcName.length() > 1)
funcName.append(" ");
std::cout << std::setprecision(3) << "Func: " << funcName << " Elapsed time: " << GetElapsedTimeInDbl() << " seconds" << std::endl;
}
// Prints ET in most reasonable format from nanoseconds to minutes
// Optionally prints a passed in message
void HiResTimer::_AutoPrintElapsedTime(std::string msg)
{
Stop();
auto type = " NanoSeconds";
auto tmp = GetTimeInNSecs();
if (tmp > 2000)
{
tmp = GetTimeInUSecs();
type = " MicroSeconds";
if (tmp > 2000)
{
tmp = GetTimeInMSecs();
type = " MilliSeconds";
if (tmp > 2000)
{
tmp = GetTimeInSecs();
type = type = " Seconds";
if (tmp > 120) // 2 minutes
{
tmp = GetTimeInMinutes();
type = type = " Minutes";
}
}
}
}
std::cout << "Func: " << msg << " Elapsed time: " << tmp << type << std::endl;
}
DurationSecs HiResTimer::GetTimeInUSecs(void)
{
return std::chrono::duration_cast<std::chrono::microseconds>(mEnd - mStart).count();
}
DurationSecs HiResTimer::GetTimeInNSecs(void)
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(mEnd - mStart).count();
}
DurationSecs HiResTimer::GetTimeInSecs(void)
{
return std::chrono::duration_cast<std::chrono::seconds>(mEnd - mStart).count();
}
DurationSecs HiResTimer::GetTimeInMinutes(void)
{
return std::chrono::duration_cast<std::chrono::minutes>(mEnd - mStart).count();
}
DurationSecs HiResTimer::GetTimeInMSecs(void)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(mEnd - mStart).count();
}
double HiResTimer::GetElapsedTimeInDbl(void)
{
return std::chrono::duration_cast<DurationDbl>(mEnd - mStart).count();
}
double HiResTimer::GetDeltaTimeInDbl(TimePoint &start, TimePoint &end)
{
return std::chrono::duration_cast<DurationDbl>(end - start).count();
}
// A standard C++, platform independent sleep method
// provided for portability and convenience
void HiResTimer::HRSleep(milliSecs mSec)
{
std::this_thread::sleep_for(milliSecs(mSec));
};
// Returns a duration delta between 2 timepoints of the most appropriate type
// Note, it does not break types on exact boundaries but a bit beyond (2000 units).
// This is because the loss of precision may not be desired.
// e.g. If the delta is 1743 nanoseconds, the end user would probably prefer
// seeing that precise value instead of 2 microseconds
DurationSecs HiResTimer::GetDeltaTime(TimePoint &start, TimePoint &end, std::string &type)
{
DurationSecs tp;
Stop();
type = " NanoSeconds";
tp = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
if (tp > 2000)
{
tp = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();;
type = " MicroSeconds";
if (tp > 2000)
{
tp = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();;
type = " MilliSeconds";
if (tp > 2000)
{
tp = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();;
type = " Seconds";
if (tp > 120) // 2 minutes
{
tp = std::chrono::duration_cast<std::chrono::minutes>(end - start).count();;
type = " Minutes";
}
}
}
}
return tp;
}
}