CbmRoot
L1Timer.h
Go to the documentation of this file.
1 #ifndef _L1Timer_H
2 #define _L1Timer_H
3 
4 /*
5  *=====================================================
6  *
7  * Authors: I.Kulakov
8  *
9  */
10 
11 #include "../CbmL1Def.h"
12 
13 #include <iomanip>
14 #include <iostream>
15 #include <string>
16 #include <vector>
17 
18 using std::cout;
19 using std::endl;
20 using std::ios;
21 using std::setw;
22 using std::string;
23 using std::vector;
24 
25 class TimerInfo {
26 public:
27  TimerInfo() : fName(""), fReal(0), fCpu(0) {};
28  TimerInfo(const string& name) : fName(name), fReal(0), fCpu(0) {};
29 
30  TimerInfo& operator=(TStopwatch& sw) {
31  fReal = sw.RealTime();
32  fCpu = sw.CpuTime();
33  return (*this);
34  };
35  TimerInfo& operator+=(TStopwatch& sw) {
36  fReal += sw.RealTime();
37  fCpu += sw.CpuTime();
38  return (*this);
39  };
40  void operator+=(const TimerInfo& t) {
41  fReal += t.fReal;
42  fCpu += t.fCpu;
43  }
44  TimerInfo operator/(const float f) const {
45  TimerInfo r;
46  r.fName = fName;
47  r.fReal = fReal / f;
48  r.fCpu = fCpu / f;
49  return r;
50  }
51 
52  // void Print(){ cout << fReal << "/" << fCpu; };
53  void PrintReal() { cout << fReal; };
54  float Real() { return fReal; };
55  string& Name() { return fName; };
56 
57 private:
58  string fName;
59  float fReal, fCpu;
60 };
61 
63 public:
65  void Add(string name) {
66  fNameToI[name] = fTIs.size();
67  fTIs.push_back(TimerInfo(name));
68  };
69  TimerInfo& operator[](string name) { return fTIs[fNameToI[name]]; };
70  TimerInfo& operator[](int i) { return fTIs[i]; };
72  for (unsigned int i = 0; i < fTIs.size(); ++i)
73  fTIs[i] += t[i];
74  }
77  r.fNameToI = fNameToI;
78  r.fTIs.resize(fTIs.size());
79  for (unsigned int i = 0; i < fTIs.size(); ++i) {
80  r.fTIs[i] = fTIs[i] / f;
81  }
82  return r;
83  }
84 
85  void PrintReal(int f = 0) {
86  if (f) {
87  PrintNames();
88  cout << endl;
89  }
90  fTIs[0].PrintReal();
91  for (unsigned int i = 1; i < fTIs.size(); ++i) {
92  cout << " | " << setw(fTIs[i].Name().size());
93  fTIs[i].PrintReal();
94  }
95  if (f) cout << endl;
96  };
97  void PrintNames() {
98  cout << fTIs[0].Name();
99  for (unsigned int i = 1; i < fTIs.size(); ++i) {
100  cout << " | " << fTIs[i].Name();
101  }
102  };
103 
104 private:
105  map<string, int> fNameToI;
106  vector<TimerInfo> fTIs;
107 };
108 
110 public:
112  void SetNIter(int n) { fTIIs.resize(n); };
113 
114  void Add(string name) {
115  for (unsigned int i = 0; i < fTIIs.size(); ++i)
116  fTIIs[i].Add(name);
117  fTIAll.Add(name);
118  }; // use after setniter
120  L1CATFIterTimerInfo& operator[](int i) { return fTIIs[i]; };
122  for (unsigned int i = 0; i < fTIIs.size(); ++i)
123  fTIIs[i] += t[i];
124  fTIAll += t.GetAllInfo();
125  }
127  L1CATFTimerInfo r;
128  r.fTIAll = fTIAll / f;
129  r.SetNIter(fTIIs.size());
130  for (unsigned int i = 0; i < fTIIs.size(); ++i) {
131  r.fTIIs[i] = fTIIs[i] / f;
132  }
133  return r;
134  }
135 
136  void Calc() {
137  fTIAll = fTIIs[0];
138  for (unsigned int i = 1; i < fTIIs.size(); ++i)
139  fTIAll += fTIIs[i];
140  }
141 
143  void PrintReal() {
144  cout.precision(1);
145  cout.setf(ios::fixed);
146  cout << " stage "
147  << " : ";
148  fTIAll.PrintNames();
149  cout << endl;
150  for (unsigned int i = 0; i < fTIIs.size(); ++i) {
151  cout << " iter " << i << " : ";
152  fTIIs[i].PrintReal();
153  cout << endl;
154  }
155  cout << " all "
156  << " : ";
157  fTIAll.PrintReal();
158  cout << endl;
159  };
160 
161 private:
162  vector<L1CATFIterTimerInfo> fTIIs;
164 };
165 
166 #endif
L1CATFTimerInfo::fTIIs
vector< L1CATFIterTimerInfo > fTIIs
Definition: L1Timer.h:159
L1CATFIterTimerInfo::fTIs
vector< TimerInfo > fTIs
Definition: L1Timer.h:106
f
float f
Definition: L1/vectors/P4_F32vec4.h:24
L1CATFIterTimerInfo::L1CATFIterTimerInfo
L1CATFIterTimerInfo()
Definition: L1Timer.h:64
L1CATFIterTimerInfo::operator+=
void operator+=(L1CATFIterTimerInfo &t)
Definition: L1Timer.h:71
TimerInfo::operator=
TimerInfo & operator=(TStopwatch &sw)
Definition: L1Timer.h:30
L1CATFTimerInfo::GetTimerAll
L1CATFIterTimerInfo & GetTimerAll()
Definition: L1Timer.h:119
TimerInfo::fName
string fName
Definition: L1Timer.h:55
TimerInfo::Real
float Real()
Definition: L1Timer.h:54
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
TimerInfo::TimerInfo
TimerInfo(const string &name)
Definition: L1Timer.h:28
L1CATFTimerInfo::SetNIter
void SetNIter(int n)
Definition: L1Timer.h:112
L1CATFTimerInfo::Calc
void Calc()
Definition: L1Timer.h:136
TimerInfo::fReal
float fReal
Definition: L1Timer.h:59
L1CATFTimerInfo::Add
void Add(string name)
Definition: L1Timer.h:114
L1CATFTimerInfo::operator[]
L1CATFIterTimerInfo & operator[](int i)
Definition: L1Timer.h:120
TimerInfo::operator+=
void operator+=(const TimerInfo &t)
Definition: L1Timer.h:40
L1CATFIterTimerInfo
Definition: L1Timer.h:62
L1CATFIterTimerInfo::operator/
L1CATFIterTimerInfo operator/(float f)
Definition: L1Timer.h:75
TimerInfo::fCpu
float fCpu
Definition: L1Timer.h:59
L1CATFTimerInfo::GetAllInfo
L1CATFIterTimerInfo & GetAllInfo()
Definition: L1Timer.h:142
TimerInfo::TimerInfo
TimerInfo()
Definition: L1Timer.h:27
L1CATFTimerInfo::fTIAll
L1CATFIterTimerInfo fTIAll
Definition: L1Timer.h:163
TimerInfo::operator+=
TimerInfo & operator+=(TStopwatch &sw)
Definition: L1Timer.h:35
L1CATFIterTimerInfo::operator[]
TimerInfo & operator[](string name)
Definition: L1Timer.h:69
TimerInfo::Name
string & Name()
Definition: L1Timer.h:55
L1CATFTimerInfo::operator/
L1CATFTimerInfo operator/(float f)
Definition: L1Timer.h:126
L1CATFTimerInfo::L1CATFTimerInfo
L1CATFTimerInfo()
Definition: L1Timer.h:111
TimerInfo::operator/
TimerInfo operator/(const float f) const
Definition: L1Timer.h:44
L1CATFIterTimerInfo::fNameToI
map< string, int > fNameToI
Definition: L1Timer.h:102
L1CATFTimerInfo
Definition: L1Timer.h:109
L1CATFTimerInfo::operator+=
void operator+=(L1CATFTimerInfo &t)
Definition: L1Timer.h:121
L1CATFIterTimerInfo::Add
void Add(string name)
Definition: L1Timer.h:65
TimerInfo
Definition: L1Timer.h:25
L1CATFIterTimerInfo::PrintNames
void PrintNames()
Definition: L1Timer.h:97
L1CATFIterTimerInfo::operator[]
TimerInfo & operator[](int i)
Definition: L1Timer.h:70
L1CATFIterTimerInfo::PrintReal
void PrintReal(int f=0)
Definition: L1Timer.h:85
TimerInfo::PrintReal
void PrintReal()
Definition: L1Timer.h:53
L1CATFTimerInfo::PrintReal
void PrintReal()
Definition: L1Timer.h:143