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