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