-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChrono.cpp
executable file
·150 lines (128 loc) · 3.77 KB
/
Chrono.cpp
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
/* ============================================================
University of Illinois at Urbana Champaign
Breadth-First-Search Optimized
Author: Luis Remis
Date: Apr 5 2015
============================================================ */
#include "Chrono.h"
#include <cmath>
#include <iostream>
#include <sstream>
using namespace std;
// *****************************************************************************
// Public methods definitions
// *****************************************************************************
Chrono::Chrono(const string& name, const bool asyncEnabled) :
name (name)
,enabled (true)
{
elapsedStats.name = "elapsedStats";
periodStats.name = "periodStats";
reset();
}
Chrono::~Chrono(void)
{
}
void Chrono::tic(void)
{
if (!enabled){
return;
}
if (ticIdle){
ticIdle = false;
doTic();
} else{
++errors;
cerr << "Chrono::tic - " << name << ": Calling Chrono::tic with no matching Chrono::tag!" << endl;
}
}
void Chrono::tac(void)
{
if (!enabled){
return;
}
if (!ticIdle){
ticIdle = true;
doTac();
} else{
++errors;
cerr << "Chrono::tac - " << name << ": Calling Chrono::tac with no matching Chrono::tic!" << endl;
}
}
void Chrono::reset(void)
{
ticIdle = true;
errors = 0;
resetStats(elapsedStats);
resetStats(periodStats);
}
void Chrono::setEnabled(const bool val)
{
enabled = val;
}
const Chrono::ChronoStats& Chrono::getElapsedStats(void) const
{
return elapsedStats;
}
const Chrono::ChronoStats& Chrono::getPeriodStats(void) const
{
return periodStats;
}
std::ostream& Chrono::printStats(const Chrono::ChronoStats& stats, std::ostream& os) const
{
os.precision(2);
os << fixed;
os << name << ": " << stats.name << endl;
os << "\terrors: " << errors << endl;
os << "\ttotalTime: " << stats.totalTime_ms << " [ms]" << endl;
os << "\taverageTime: " << stats.averageTime_ms << " [ms]" << endl;
os << "\tstdDevTime: " << stats.stdDevTime_ms << " [ms]" << endl;
os << "\tlastTime: " << stats.lastTime_ms << " [ms]" << endl;
os << "\tminTime: " << stats.minTime_ms << " [ms]" << endl;
os << "\tmaxTime: " << stats.maxTime_ms << " [ms]" << endl;
return os;
}
std::ostream& Chrono::printAvgTime(const Chrono::ChronoStats& stats, std::ostream& os) const
{
os.precision(2);
os << fixed;
os << name << ": " << stats.name << " -> " << "averageTime: " << stats.averageTime_ms << " [ms]" << endl;
return os;
}
std::ostream& Chrono::printAvgTime(const Chrono::ChronoStats& stats, std::ostream& os, const float ref) const
{
os.precision(2);
os << fixed;
os << name << ": " << stats.name << " -> " << "averageTime: " << stats.averageTime_ms << " [ms] (";
os << (stats.averageTime_ms/ref*100.0f) << "%)" << endl;
return os;
}
// *****************************************************************************
// Private/Protected methods definitions
// *****************************************************************************
void Chrono::resetStats(ChronoStats& stats)
{
stats.counter = 0;
stats.totalTime_ms = 0.0f;
stats.totalSquaredTime_ms2 = 0.0f;
stats.averageTime_ms = 0.0f;
stats.stdDevTime_ms = 0.0f;
stats.lastTime_ms = 0.0f;
stats.minTime_ms = 0.0f;
stats.maxTime_ms = 0.0f;
}
void Chrono::updateStats(ChronoStats& stats)
{
++stats.counter;
stats.totalTime_ms += stats.lastTime_ms;
stats.totalSquaredTime_ms2 += stats.lastTime_ms * stats.lastTime_ms;
stats.averageTime_ms = stats.totalTime_ms / (float)stats.counter;
stats.stdDevTime_ms = sqrtf(stats.totalSquaredTime_ms2 / (float)stats.counter - stats.averageTime_ms * stats.averageTime_ms);
if (stats.counter > 1){
stats.maxTime_ms = max(stats.lastTime_ms, stats.maxTime_ms);
stats.minTime_ms = min(stats.lastTime_ms, stats.minTime_ms);
} else{
stats.maxTime_ms = stats.lastTime_ms;
stats.minTime_ms = stats.lastTime_ms;
}
}