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