1 #ifndef UTIL_CLOCK_MAN_INDIVIDUAL_RECORDS_H_INCLUDED
2 #define UTIL_CLOCK_MAN_INDIVIDUAL_RECORDS_H_INCLUDED
42 template<
typename T = std::chrono::seconds>
55 unsigned int GetCount(
const std::string& name)
const
57 return IsPresent(name) ? (m_map.find(name)->second).size() : 0;
63 Duration cumul = T::duration::zero();
65 std::vector<Duration>
const& v = m_map.find(name)->second;
66 cumul = std::accumulate(v.begin(), v.end(), T::duration::zero());
72 Duration
GetMean(
const std::string& name)
const
74 Duration cumul = T::duration::zero();
75 unsigned int const count =
GetCount(name);
77 std::vector<Duration>
const& v = m_map.find(name)->second;
78 cumul = std::accumulate(v.begin(), v.end(), T::duration::zero());
87 Duration minimum = T::duration::zero();
89 std::vector<Duration>
const& v = m_map.find(name)->second;
92 for (
auto const& el : v) {
93 minimum = ( (minimum <= el) ? minimum : el );
103 std::list<std::string> l;
104 for (
auto const& p : m_map) {
105 l.push_back(p.first);
111 std::vector<Duration>
GetRecord(
const std::string& name)
const
113 std::vector<Duration> v;
115 v = m_map.find(name)->second;
121 template <
typename U = std::chrono::seconds>
125 for (
auto const& p : m_map) {
126 for (
auto const& v : p.second) {
127 rec.
Record(p.first, std::chrono::duration_cast<U>(v));
142 Duration deviation = T::duration::zero();
144 std::vector<Duration>
const& v = m_map.find(name)->second;
145 Duration
const mean =
GetMean(name);
146 typename Duration::rep cov = 0;
147 for (
auto const& e : v) {
148 typename Duration::rep
const x = (e-mean).count();
151 typename Duration::rep
const x = std::sqrt(cov);
152 deviation = Duration(x);
160 return (m_map.find(name) != m_map.end());
167 for (
auto const& name : extra.
GetNames()) {
168 for (
auto const& elem : extra.
GetRecord(name)) {
169 Record(name, std::chrono::duration_cast<T>(elem));
177 for (
auto const& name : extra.
GetNames()) {
179 m_map[name].insert(m_map[name].end(),
180 extra.m_map.at(name).begin(), extra.m_map.at(name).end());
182 m_map[name] = extra.m_map.at(name);
188 template<
typename R,
typename P>
189 void Record(
const std::string& name,
const std::chrono::duration<R, P>& duration)
191 m_map[name].push_back(std::chrono::duration_cast<T>(duration));
195 std::map<std::string, std::vector<Duration>> m_map;
207 os << right <<
"timings: " << endl
208 << setw(14) <<
"name" <<
" | "
209 << setw(25) <<
"(hh:mm:ss: ms: us) | (ms) | "
210 << setw(7) <<
"count" <<
" | "
211 << setw(25) <<
"(hh:mm:ss: ms: us) | (ms) | " << endl
212 << string(80,
'-') << endl;
216 for (
auto const& name : name_list) {
217 auto const count = dr.
GetCount(name);
219 auto const cumul = duration_cast<microseconds>(cumul_val);
220 auto const avg_val = (count != 0) ? (cumul_val / count) : microseconds::zero();
221 auto const avg = duration_cast<microseconds>(avg_val);
224 << setw(14) << name <<
" | "
226 << setw(8) << scientific << setprecision(4) << cumul.count() <<
" | "
227 << setw(7) << count <<
" | "
229 << setw(8) << scientific << setprecision(4) << avg.count() <<
" | "<< endl;
243 os << right <<
"timings: " << endl
244 << setw(14) <<
"name" <<
" | "
245 << setw(22) <<
"(hh:mm:ss: ms) | (ms) | "
246 << setw(7) <<
"count" <<
" | "
247 << setw(22) <<
"(hh:mm:ss: ms) | (ms) | " << endl
248 << string(80,
'-') << endl;
252 for (
auto const& name : name_list) {
253 auto const count = dr.
GetCount(name);
255 auto const cumul = duration_cast<milliseconds>(cumul_val);
256 auto const avg_val = (count != 0) ? (cumul_val / count) : milliseconds::zero();
257 auto const avg = duration_cast<milliseconds>(avg_val);
260 << setw(14) << name <<
" | "
262 << setw(8) << scientific << setprecision(4) << cumul.count() <<
" | "
263 << setw(7) << count <<
" | "
265 << setw(8) << scientific << setprecision(4) << avg.count() <<
" | "<< endl;
279 os << right <<
"timings: " << endl
280 << setw(14) <<
"name" <<
" | "
281 << setw(20) <<
" (hh:mm:ss) | (s) | "
282 << setw(7) <<
"count" <<
" | "
283 << setw(20) <<
" (hh:mm:ss) | (s) | " << endl
284 << string(76,
'-') << endl;
288 for (
auto const& name : name_list) {
289 auto const count = dr.
GetCount(name);
291 auto const cumul = duration_cast<seconds>(cumul_val);
292 auto const avg_val = (count != 0) ? (cumul_val / count) : seconds::zero();
293 auto const avg = duration_cast<seconds>(avg_val);
296 << setw(14) << name <<
" | "
298 << setw(7) << scientific << setprecision(4) << cumul.count() <<
" | "
299 << setw(7) << count <<
" | "
301 << setw(7) << scientific << setprecision(4) << avg.count() <<
" | "<< endl;
310 inline std::ostream& operator<<(std::ostream& os, IndividualRecords<T>
const& dr)
315 os << right <<
"duration records: name | mean | stddev" << endl;
317 auto name_list = dr.GetNames();
319 for (
auto const& name : name_list) {
320 auto const mean = dr.GetMean(name);
321 auto const stddev = dr.GetStandardDeviation(name);
324 << setw(15) << name <<
" | "
326 << setw(10) << scientific << setprecision(4) << mean.count() <<
" | "
328 << setw(10) << scientific << setprecision(4) << stddev.count() << endl;
336 #endif // end-of-include-guard
IndividualRecords< Duration > GetRecords() const
Return records for all names in durations Duration (so no casting).
Interface for TimeKeeper::Utils.
std::list< std::string > GetNames() const
Return list of names.
static std::string ToColonString(std::chrono::seconds d)
Procude string in hh:mm:ss format.
Duration GetMinimum(const std::string &name) const
Return minimum time for records with name.
std::vector< Duration > GetRecord(const std::string &name) const
Return records associated with given name.
Namespace for the core simulator.
IndividualRecords< U > GetRecords() const
Return records for all names in durations U (casting if required).
Duration GetStandardDeviation(const std::string &name) const
Return standard deviation for record with given name.
void Merge(const IndividualRecords< Duration > &extra)
Merge an extra set of records of same Duration (no casting).
void Clear()
Clear the entire set of records.
void Record(const std::string &name, const std::chrono::duration< R, P > &duration)
Record the duration for the given name.
Duration GetMean(const std::string &name) const
Return cumulative time for records with name.
unsigned int GetCount(const std::string &name) const
Return count for records with name.
bool IsPresent(const std::string &name) const
Return whether there are records associated with a given name.
Duration GetCumulative(const std::string &name) const
Return cumulative time for records with name.
std::ostream & operator<<(std::ostream &os, CumulativeRecords< std::chrono::nanoseconds > const &dr)
Nicely formated output with nanoseconds.
void Merge(const IndividualRecords< U > &extra)
Merge an extra set of records (casting durations if required).
Utility class to record durations.