NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
Timer.cxx
Go to the documentation of this file.
1//***************************************************************************************
4//***************************************************************************************
5
6#include "NAGASH/Timer.h"
7
8using namespace std;
9using namespace NAGASH;
10
18void Timer::PrintCurrent(const TString &prefix)
19{
20 auto titleguard = MSGUser()->StartTitleWithGuard(TimerName + "::PrintCurrent");
21 auto now = std::chrono::system_clock::now();
22 auto t_c = std::chrono::system_clock::to_time_t(now);
23 MSGUser()->MSG(MSGLevel::INFO, prefix, std::put_time(std::localtime(&t_c), "%F %T"));
24}
25
28void Timer::Record(const TString &Name)
29{
30 if (TimeIndexMap.find(Name) == TimeIndexMap.end())
31 {
32 TimeIndexMap.emplace(pair<TString, int>(Name, TimeRecord.size()));
33 TimeRecordName.emplace_back(Name);
34 TimeRecord.emplace_back(std::chrono::system_clock::now());
35 }
36 else
37 {
38 auto guard = MSGUser()->StartTitleWithGuard("Timer::Record");
39 MSGUser()->MSG_ERROR("duplicate record name ", Name, ", this won't be recorded");
40 }
41}
42
46void Timer::Duration(const TString &Name1, const TString &Name2)
47{
48 MSGUser()->StartTitle(TimerName + "::Duration");
49 auto r1 = TimeIndexMap.find(Name1);
50 auto r2 = TimeIndexMap.find(Name2);
51 if (r1 != TimeIndexMap.end() && r2 != TimeIndexMap.end())
52 {
53 int id1 = r1->second, id2 = r2->second;
54 if (id1 < id2)
55 std::swap(id1, id2);
56
57 double duration_h = std::chrono::duration_cast<std::chrono::hours>(TimeRecord[id1] - TimeRecord[id2]).count();
58 double duration_m = std::chrono::duration_cast<std::chrono::minutes>(TimeRecord[id1] - TimeRecord[id2]).count();
59 double duration_s = std::chrono::duration_cast<std::chrono::seconds>(TimeRecord[id1] - TimeRecord[id2]).count();
60 double duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(TimeRecord[id1] - TimeRecord[id2]).count();
61 double duration_us = std::chrono::duration_cast<std::chrono::microseconds>(TimeRecord[id1] - TimeRecord[id2]).count();
62 int duration_h_int = (int)duration_h;
63 int duration_m_int = (int)duration_m;
64 int duration_s_int = (int)duration_s;
65 int duration_ms_int = (int)duration_ms;
66 if (duration_ms_int == 0)
67 {
68 MSGUser()->MSG(MSGLevel::INFO, "Duration between ", Name1, " and ", Name2, " : ", duration_us, " us");
69 }
70 else if (duration_s_int == 0)
71 {
72 MSGUser()->MSG(MSGLevel::INFO, "Duration between ", Name1, " and ", Name2, " : ", duration_ms, " ms");
73 }
74 else if (duration_m_int == 0)
75 {
76 duration_ms_int = duration_ms_int % 1000;
77 double duration_double = duration_s + duration_ms_int / 1000.0;
78 MSGUser()->MSG(MSGLevel::INFO, "Duration between ", Name1, " and ", Name2, " : ", duration_double, " s");
79 }
80 else if (duration_h_int == 0)
81 {
82 duration_s_int = duration_s_int % 60;
83 MSGUser()->MSG(MSGLevel::INFO, "Duration between ", Name1, " and ", Name2, " : ", duration_m_int, " m ", duration_s_int, " s");
84 }
85 else
86 {
87 duration_m_int = duration_m_int % 60;
88 MSGUser()->MSG(MSGLevel::INFO, "Duration between ", Name1, " and ", Name2, " : ", duration_h_int, " h ", duration_m_int, " m");
89 }
90 }
91
92 if (r1 == TimeIndexMap.end())
93 MSGUser()->MSG(MSGLevel::WARNING, "Record ", Name1, " not found");
94 if (r2 == TimeIndexMap.end())
95 MSGUser()->MSG(MSGLevel::WARNING, "Record ", Name2, " not found");
96
97 MSGUser()->EndTitle();
98}
99
102{
103 TimeRecord.clear();
104 TimeIndexMap.clear();
105 TimeRecordName.clear();
106}
std::vector< TString > TimeRecordName
Definition Timer.h:28
void PrintCurrent(const TString &prefix="")
Print the current time.
Definition Timer.cxx:18
void Duration(const TString &Name1, const TString &Name2)
Print the time interval between two time stamps.
Definition Timer.cxx:46
std::map< TString, int > TimeIndexMap
Definition Timer.h:27
std::vector< std::chrono::time_point< std::chrono::system_clock > > TimeRecord
Definition Timer.h:26
TString TimerName
Definition Timer.h:29
void Record(const TString &Name)
Record the current time.
Definition Timer.cxx:28
void Clear()
Clear all the time stamps.
Definition Timer.cxx:101
std::shared_ptr< MSGTool > MSGUser()
return the MSGTool inside.
Definition Tool.h:91