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