23 #ifndef COM_DAFER45_TBTK_TIMER
24 #define COM_DAFER45_TBTK_TIMER
81 static void tick(std::string tag =
"");
100 static void tick(
unsigned int id);
105 static void tock(
unsigned int id);
119 static std::vector<std::chrono::time_point<std::chrono::high_resolution_clock>> timestamps;
122 static std::vector<std::string> tags;
125 static std::vector<std::chrono::time_point<std::chrono::high_resolution_clock>> accumulatorTimestamps;
128 static std::vector<std::string> accumulatorTags;
131 static std::vector<long> accumulators;
135 timestamps.push_back(std::chrono::high_resolution_clock::now());
140 std::chrono::time_point<std::chrono::high_resolution_clock> stop = std::chrono::high_resolution_clock::now();
141 if(timestamps.size() > 0){
142 std::chrono::time_point<std::chrono::high_resolution_clock> start = timestamps.back();
143 timestamps.pop_back();
144 std::string tag = tags.back();
147 int hours = (std::chrono::duration_cast<std::chrono::hours>(stop - start).count());
148 int minutes = (std::chrono::duration_cast<std::chrono::minutes>(stop - start).count())%60;
149 int seconds = (std::chrono::duration_cast<std::chrono::seconds>(stop - start).count())%60;
150 int milliseconds = (std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count())%1000;
151 int microseconds = (std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count())%1000;
152 int nanoseconds = (std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count())%1000;
157 if(hours > 0 || minutes > 0)
159 if(hours > 0 || minutes > 0 || seconds > 0)
161 if(hours > 0 || minutes > 0 || seconds > 0 || milliseconds > 0)
163 if(hours > 0 || minutes > 0 || seconds > 0 || milliseconds > 0 || microseconds > 0)
165 if(hours > 0 || minutes > 0 || seconds > 0 || milliseconds > 0 || microseconds > 0 || nanoseconds > 0)
170 Streams::out <<
"Error in Time::tock(): No corresponding tick call made.\n";
175 accumulatorTimestamps.push_back(
176 std::chrono::high_resolution_clock::now()
178 accumulatorTags.push_back(tag);
179 accumulators.push_back(0);
181 return accumulators.size() - 1;
186 id < accumulators.size(),
188 "'id' is out of bounds.",
189 "Ensure that the id corresponds to a value returned by a"
190 <<
" corresponding call to Timer::createAccumulator()."
192 accumulatorTimestamps[id] = std::chrono::high_resolution_clock::now();
197 id < accumulators.size(),
199 "'id' is out of bounds.",
200 "Ensure that the id corresponds to a value returned by a"
201 <<
" corresponding call to Timer::createAccumulator()."
203 std::chrono::time_point<std::chrono::high_resolution_clock> stop
204 = std::chrono::high_resolution_clock::now();
205 std::chrono::time_point<std::chrono::high_resolution_clock> start
206 = accumulatorTimestamps[id];
209 += std::chrono::duration_cast<std::chrono::nanoseconds>(
216 id < accumulators.size(),
217 "Timer::resetAccumulator()",
218 "'id' is out of bounds.",
219 "Ensure that the id corresponds to a value returned by a"
220 <<
" corresponding call to Timer::createAccumulator()."
223 accumulators[id] = 0;
227 for(
unsigned int n = 0; n < accumulators.size(); n++)
232 Streams::out <<
"============================== Accumulator table ==============================\n";
233 Streams::out << std::left << std::setw(10) <<
"ID" << std::setw(33) <<
"Time" << std::setw(100) <<
" Tag" <<
"\n";
234 for(
unsigned int n = 0; n < accumulators.size(); n++){
235 long time = accumulators[n];
237 long hours = time/(60ll*60ll*1000ll*1000ll*1000ll);
238 long minutes = (time/(60ll*1000ll*1000ll*1000ll))%60ll;
239 long seconds = (time/(1000ll*1000ll*1000ll))%60ll;
240 long milliseconds = (time/(1000ll*1000ll))%1000ll;
241 long microseconds = (time/(1000ll))%1000ll;
242 long nanoseconds = time%1000ll;
244 const std::string &tag = accumulatorTags[n];
246 Streams::out << std::left << std::setw(10) <<
"[" + std::to_string(n) +
"]" << std::right;
248 Streams::out << std::setw(6) << std::to_string(hours) +
"h";
251 if(hours > 0 || minutes > 0)
252 Streams::out << std::setw(5) << std::to_string(minutes) +
"m";
255 if(hours > 0 || minutes > 0 || seconds > 0)
256 Streams::out << std::setw(4) << std::to_string(seconds) +
"s";
259 if(hours > 0 || minutes > 0 || seconds > 0 || milliseconds > 0)
260 Streams::out << std::setw(6) << std::to_string(milliseconds) +
"ms";
263 if(hours > 0 || minutes > 0 || seconds > 0 || milliseconds > 0 || microseconds > 0)
264 Streams::out << std::setw(6) << std::to_string(microseconds) +
"us";
267 if(hours > 0 || minutes > 0 || seconds > 0 || milliseconds > 0 || microseconds > 0 || nanoseconds > 0)
268 Streams::out << std::setw(6) << std::to_string(nanoseconds) +
"ns";
271 Streams::out << std::left <<
" " << std::setw(100) << tag <<
"\n";
273 Streams::out <<
"===============================================================================\n";