-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChronoTimer.cc
255 lines (233 loc) · 8.26 KB
/
ChronoTimer.cc
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*******************************************************************************
* Copyright (c) 2020 CEA
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
* Contributors: Francois Letierce
*******************************************************************************/
#include "ChronoTimer.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cmath>
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Ctor.
* Every inner members are default constructed.
* Timer is stopped at its creation.
*/
/*---------------------------------------------------------------------------*/
ChronoTimer::
ChronoTimer(bool start_on_create)
: m_state(eTimerState::kSTOPPED), m_start_time(clock_type::time_point()), m_time(duration_type::zero()),
m_mean_time(duration_type::zero()), m_min_time(duration_type::zero()), m_max_time(duration_type::zero()),
m_weight(1)
{
if (start_on_create)
this->start();
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Accessor of time value.
*
* @return : time between timer start/stop operations.
*/
/*---------------------------------------------------------------------------*/
const ChronoTimer::duration_type& ChronoTimer::
time() const
{
return m_time;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Accessor of mean time value.
*
* @return : mean time between all start/stop operations.
*/
/*---------------------------------------------------------------------------*/
const ChronoTimer::duration_type& ChronoTimer::
meanTime() const {
return m_mean_time;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Accessor of minimum time value.
*
* @return : minimum time recorded between start/stop operations.
*/
/*---------------------------------------------------------------------------*/
const ChronoTimer::duration_type& ChronoTimer::
minTime() const {
return m_min_time;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Accessor of maximum time value.
*
* @return : maximum time recorded between start/stop operations.
*/
/*---------------------------------------------------------------------------*/
const ChronoTimer::duration_type& ChronoTimer::
maxTime() const {
return m_max_time;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Starts timer, recording time point and updating internal clock state.
*/
/*---------------------------------------------------------------------------*/
void ChronoTimer::
start()
{
if (m_state == eTimerState::kSTOPPED) {
// Recording starting time
m_start_time = clock_type::now();
// Updating state
m_state = eTimerState::kSTARTED;
} else {
// If a timer is already running, we ignore the new start command and print a warning
std::cerr << "### WARNING: trying to start an already started timer, ignoring timer.start()..." << std::endl;
}
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Stops timer, computing duration between start and stop commands, mean,
* min, max time values and updating internal clock state.
*/
/*---------------------------------------------------------------------------*/
void ChronoTimer::
stop()
{
if (m_state == eTimerState::kSTARTED) {
// Current time value time between the last start and this stop
duration_type current_time(std::chrono::duration_cast<duration_type>(clock_type::now() - m_start_time));
// Global time
m_time += current_time;
// Mean
m_mean_time = std::chrono::duration_cast<duration_type>(((m_mean_time * m_weight + current_time) / (m_weight + 1)));
++m_weight;
// Min
if (m_min_time == duration_type::zero() || current_time < m_min_time)
m_min_time = current_time;
// Max
if (m_max_time == duration_type::zero() || current_time > m_max_time)
m_max_time = current_time;
// Updating state
m_state = eTimerState::kSTOPPED;
} else {
// If a timer is already stopped, we ignore the new stop command and print a warning
std::cerr << "### WARNING: trying to stop an already stopped timer, ignoring timer.stop()..." << std::endl;
}
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Reseting starting time point and time value (without updating timer state).
*/
/*---------------------------------------------------------------------------*/
void ChronoTimer::
reset()
{
m_start_time = clock_type::time_point();
m_time = duration_type::zero();
m_mean_time = duration_type::zero();
m_min_time = duration_type::zero();
m_max_time = duration_type::zero();
m_weight = 1;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Pretty printer for human.
* @param time : time to print
* @param short_version : flag for detailled information
* @return : string filled with time information
*/
/*---------------------------------------------------------------------------*/
const std::string ChronoTimer::
print(const duration_type& time, const bool short_version) {
// A floating point milliseconds type
using FpMilliseconds = std::chrono::duration<float, std::chrono::milliseconds::period>;
std::chrono::hours h(0);
std::chrono::minutes m(0);
std::chrono::seconds s(0);
FpMilliseconds ms(0.0);
std::stringstream output;
if (!short_version) {
// Compute hours
h = std::chrono::duration_cast<std::chrono::hours>(time);
// Display hours and compute minutes
if (h > std::chrono::hours::zero()) {
output << std::setw(2) << std::to_string(h.count()) << "h";
m = std::chrono::duration_cast<std::chrono::minutes>(time % h);
} else {
output << std::setw(3) << " ";
m = std::chrono::duration_cast<std::chrono::minutes>(time);
}
m += h;
// Display minutes and compute seconds
if (m > std::chrono::minutes::zero()) {
output << std::setw(2) << std::to_string((m - h).count()) << "m";
s = std::chrono::duration_cast<std::chrono::seconds>(time % m);
} else {
output << std::setw(3) << " ";
s = std::chrono::duration_cast<std::chrono::seconds>(time);
}
s += m;
} else {
// Compute seconds
s = std::chrono::duration_cast<std::chrono::seconds>(time);
}
// Display seconds and compute milliseconds
if (s > std::chrono::seconds::zero()) {
output << (short_version?std::setw(5):std::setw(2)) << std::to_string((s - m).count()) << "s";
ms = std::chrono::duration_cast<FpMilliseconds>(time % s);
} else {
output << (short_version?std::setw(6):std::setw(3)) << " ";
ms = std::chrono::duration_cast<FpMilliseconds>(time);
}
ms += s;
// Display milliseconds
if (ms > std::chrono::milliseconds::zero())
output << std::setw(3) << std::to_string(static_cast<int>(std::ceil((ms - s).count()))) << "ms";
return output.str();
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Pretty printer for human.
* @return : string filled with time information
*/
/*---------------------------------------------------------------------------*/
const std::string ChronoTimer::
print(const bool short_version) {
return print(m_time, short_version);
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
* Pretty printer for human.
* @return : string filled with all timers information
*/
/*---------------------------------------------------------------------------*/
const std::string ChronoTimer::
summary() {
std::stringstream output;
output << "Total Time: " << print()
<< ". ~Average: " << print(m_mean_time, true)
<< " [min: " << print(m_min_time, true)
<< ", max: " << print(m_max_time, true) << "]";
return output.str();
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/