ROOT  6.06/09
Reference Guide
TBenchmark.cxx
Go to the documentation of this file.
1 
2 /*************************************************************************
3  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
4  * All rights reserved. *
5  * *
6  * For the licensing terms see $ROOTSYS/LICENSE. *
7  * For the list of contributors see $ROOTSYS/README/CREDITS. *
8  *************************************************************************/
9 
10 #include "TBenchmark.h"
11 #include "TROOT.h"
12 #include "TStopwatch.h"
13 
14 
15 TBenchmark *gBenchmark = 0;
16 
18 
19 /** \class TBenchmark
20 This class is a ROOT utility to help benchmarking applications
21 */
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 /// Benchmark default constructor
25 
27 {
28  fNbench = 0;
29  fNmax = 20;
30  fNames = 0;
31  fRealTime = 0;
32  fCpuTime = 0;
33  fTimer = 0;
34 }
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Copy constructor.
38 
40  TNamed(bm),
41  fNbench(bm.fNbench),
42  fNmax(bm.fNmax),
43  fNames(0),
44  fRealTime(0),
45  fCpuTime(0),
46  fTimer(0)
47 {
48  fNames = new TString[fNmax];
49  fRealTime = new Float_t[fNmax];
50  fCpuTime = new Float_t[fNmax];
51  fTimer = new TStopwatch[fNmax];
52 
53  for(Int_t i = 0; i<fNmax; ++i) {
54  fNames[i] = bm.fNames[i];
55  fRealTime[i] = bm.fRealTime[i];
56  fCpuTime[i] = bm.fCpuTime[i];
57  fTimer[i] = bm.fTimer[i];
58  }
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Assignment operator.
63 
65 {
66  if (this!=&bm) {
68  fNbench=bm.fNbench;
69  fNmax=bm.fNmax;
70 
71  delete [] fNames;
72  delete [] fRealTime;
73  delete [] fCpuTime;
74  delete [] fTimer;
75 
76  fNames = new TString[fNmax];
77  fRealTime = new Float_t[fNmax];
78  fCpuTime = new Float_t[fNmax];
79  fTimer = new TStopwatch[fNmax];
80 
81  for(Int_t i = 0; i<fNmax; ++i) {
82  fNames[i] = bm.fNames[i];
83  fRealTime[i] = bm.fRealTime[i];
84  fCpuTime[i] = bm.fCpuTime[i];
85  fTimer[i] = bm.fTimer[i];
86  }
87  }
88  return *this;
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 /// Benchmark destructor.
93 
95 {
96  fNbench = 0;
97  if (fNames) { delete [] fNames; fNames = 0;}
98  if (fRealTime) { delete [] fRealTime; fRealTime = 0;}
99  if (fCpuTime) { delete [] fCpuTime; fCpuTime = 0;}
100  if (fTimer ) { delete [] fTimer; fTimer = 0;}
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// Returns index of Benchmark name.
105 
106 Int_t TBenchmark::GetBench(const char *name) const
107 {
108  for (Int_t i=0;i<fNbench;i++) {
109  if (!strcmp(name,(const char*)fNames[i])) return i;
110  }
111  return -1;
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// Returns Cpu time used by Benchmark name.
116 
118 {
119  Int_t bench = GetBench(name);
120  if (bench >= 0) return fCpuTime[bench];
121  else return 0;
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// Returns Realtime used by Benchmark name.
126 
128 {
129  Int_t bench = GetBench(name);
130  if (bench >= 0) return fRealTime[bench];
131  else return 0;
132 }
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 /// Prints parameters of Benchmark name.
136 
137 void TBenchmark::Print(const char *name) const
138 {
139  Int_t bench = GetBench(name);
140  if (bench < 0) return;
141  Printf("%-10s: Real Time = %6.2f seconds Cpu Time = %6.2f seconds",name,fRealTime[bench],fCpuTime[bench]);
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// Reset all Benchmarks
146 
148 {
149  fNbench = 0;
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// Stops Benchmark name and Prints results
154 
155 void TBenchmark::Show(const char *name)
156 {
157  Stop(name);
158  Print((char*)name);
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Starts Benchmark with the specified name.
163 ///
164 /// An independent timer (see class TStopwatch) is started.
165 /// The name of the benchmark is entered into the list of benchmarks.
166 /// Benchmark can be stopped via TBenchmark::Stop().
167 /// Results can be printed via TBenchmark::Print().
168 /// TBenchmark::Show() can be used to stop benchmark and print results.
169 /// If name is an already existing benchmark, timing will resume.
170 /// A summary of all benchmarks can be seen via TBenchmark::Summary().
171 
172 void TBenchmark::Start(const char *name)
173 {
174  if (!fNames) {
175  fNames = new TString[fNmax];
176  fRealTime = new Float_t[fNmax];
177  fCpuTime = new Float_t[fNmax];
178  fTimer = new TStopwatch[fNmax];
179  }
180  Int_t bench = GetBench(name);
181  if (bench < 0 && fNbench < fNmax ) {
182  // define a new benchmark to Start
183  fNames[fNbench] = name;
184  bench = fNbench;
185  fNbench++;
186  fTimer[bench].Reset();
187  fTimer[bench].Start();
188  fRealTime[bench] = 0;
189  fCpuTime[bench] = 0;
190  } else if (bench >= 0) {
191  // Resume the existing benchmark
192  fTimer[bench].Continue();
193  }
194  else
195  Warning("Start","too many benchmarks");
196 }
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// Terminates Benchmark with specified name.
200 
201 void TBenchmark::Stop(const char *name)
202 {
203  Int_t bench = GetBench(name);
204  if (bench < 0) return;
205 
206  fTimer[bench].Stop();
207  fRealTime[bench] = fTimer[bench].RealTime();
208  fCpuTime[bench] = fTimer[bench].CpuTime();
209 }
210 
211 ////////////////////////////////////////////////////////////////////////////////
212 /// Prints a summary of all benchmarks.
213 
215 {
216  rt = 0;
217  cp = 0;
218  for (Int_t i=0;i<fNbench;i++) {
219  Printf("%-10s: Real Time = %6.2f seconds Cpu Time = %6.2f seconds",(const char*)fNames[i],fRealTime[i],fCpuTime[i]);
220  rt += fRealTime[i];
221  cp += fCpuTime[i];
222  }
223  Printf("%-10s: Real Time = %6.2f seconds Cpu Time = %6.2f seconds","TOTAL",rt,cp);
224 }
TStopwatch * fTimer
Definition: TBenchmark.h:42
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:108
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
Float_t * fRealTime
Definition: TBenchmark.h:40
float Float_t
Definition: RtypesCore.h:53
TBenchmark & operator=(const TBenchmark &)
Assignment operator.
Definition: TBenchmark.cxx:64
Int_t fNmax
Definition: TBenchmark.h:38
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
Basic string class.
Definition: TString.h:137
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:123
int Int_t
Definition: RtypesCore.h:41
virtual void Stop(const char *name)
Terminates Benchmark with specified name.
Definition: TBenchmark.cxx:201
Int_t GetBench(const char *name) const
Returns index of Benchmark name.
Definition: TBenchmark.cxx:106
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
TString * fNames
Definition: TBenchmark.h:39
Float_t * fCpuTime
Definition: TBenchmark.h:41
Float_t GetCpuTime(const char *name)
Returns Cpu time used by Benchmark name.
Definition: TBenchmark.cxx:117
void Continue()
Resume a stopped stopwatch.
Definition: TStopwatch.cxx:91
Float_t GetRealTime(const char *name)
Returns Realtime used by Benchmark name.
Definition: TBenchmark.cxx:127
virtual ~TBenchmark()
Benchmark destructor.
Definition: TBenchmark.cxx:94
virtual void Print(Option_t *name="") const
Prints parameters of Benchmark name.
Definition: TBenchmark.cxx:137
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:40
This class is a ROOT utility to help benchmarking applications.
Definition: TBenchmark.h:33
#define Printf
Definition: TGeoToOCC.h:18
#define ClassImp(name)
Definition: Rtypes.h:279
#define name(a, b)
Definition: linkTestLib0.cpp:5
void Reset()
Definition: TStopwatch.h:54
virtual void Summary(Float_t &rt, Float_t &cp)
Prints a summary of all benchmarks.
Definition: TBenchmark.cxx:214
Int_t fNbench
Definition: TBenchmark.h:37
virtual void Reset()
Reset all Benchmarks.
Definition: TBenchmark.cxx:147
Stopwatch class.
Definition: TStopwatch.h:30
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904