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 "TStopwatch.h"
12
14
16
17/** \class TBenchmark
18\ingroup Base
19
20This 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 = nullptr;
31 fRealTime = nullptr;
32 fCpuTime = nullptr;
33 fTimer = nullptr;
34}
35
36////////////////////////////////////////////////////////////////////////////////
37/// Copy constructor.
38
40 TNamed(bm),
41 fNbench(bm.fNbench),
42 fNmax(bm.fNmax),
43 fNames(nullptr),
44 fRealTime(nullptr),
45 fCpuTime(nullptr),
46 fTimer(nullptr)
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) {
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 = nullptr;}
98 if (fRealTime) { delete [] fRealTime; fRealTime = nullptr;}
99 if (fCpuTime) { delete [] fCpuTime; fCpuTime = nullptr;}
100 if (fTimer ) { delete [] fTimer; fTimer = nullptr;}
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Returns index of Benchmark name.
105
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
137void 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
155void 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
172void 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
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
201void 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}
int Int_t
Definition: RtypesCore.h:45
float Float_t
Definition: RtypesCore.h:57
#define ClassImp(name)
Definition: Rtypes.h:364
TBenchmark * gBenchmark
Definition: TBenchmark.cxx:13
char name[80]
Definition: TGX11.cxx:110
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:137
Int_t fNmax
Definition: TBenchmark.h:34
TStopwatch * fTimer
Definition: TBenchmark.h:38
virtual void Reset()
Reset all Benchmarks.
Definition: TBenchmark.cxx:147
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:214
TBenchmark()
Benchmark default constructor.
Definition: TBenchmark.cxx:26
Int_t GetBench(const char *name) const
Returns index of Benchmark name.
Definition: TBenchmark.cxx:106
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
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:64
virtual void Stop(const char *name)
Terminates Benchmark with specified name.
Definition: TBenchmark.cxx:201
virtual ~TBenchmark()
Benchmark destructor.
Definition: TBenchmark.cxx:94
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
Float_t GetCpuTime(const char *name)
Returns Cpu time used by Benchmark name.
Definition: TBenchmark.cxx:117
Float_t GetRealTime(const char *name)
Returns Realtime used by Benchmark name.
Definition: TBenchmark.cxx:127
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:879
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:136