00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "TBenchmark.h"
00011 #include "TROOT.h"
00012 #include "TStopwatch.h"
00013
00014
00015 TBenchmark *gBenchmark = 0;
00016
00017 ClassImp(TBenchmark)
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 TBenchmark::TBenchmark(): TNamed()
00030 {
00031
00032
00033
00034 fNbench = 0;
00035 fNmax = 20;
00036 fNames = 0;
00037 fRealTime = 0;
00038 fCpuTime = 0;
00039 fTimer = 0;
00040 }
00041
00042
00043 TBenchmark::TBenchmark(const TBenchmark& bm) :
00044 TNamed(bm),
00045 fNbench(bm.fNbench),
00046 fNmax(bm.fNmax),
00047 fNames(0),
00048 fRealTime(0),
00049 fCpuTime(0),
00050 fTimer(0)
00051 {
00052
00053 fNames = new TString[fNmax];
00054 fRealTime = new Float_t[fNmax];
00055 fCpuTime = new Float_t[fNmax];
00056 fTimer = new TStopwatch[fNmax];
00057
00058 for(Int_t i = 0; i<fNmax; ++i) {
00059 fNames[i] = bm.fNames[i];
00060 fRealTime[i] = bm.fRealTime[i];
00061 fCpuTime[i] = bm.fCpuTime[i];
00062 fTimer[i] = bm.fTimer[i];
00063 }
00064 }
00065
00066
00067 TBenchmark& TBenchmark::operator=(const TBenchmark& bm)
00068 {
00069
00070 if(this!=&bm) {
00071 TNamed::operator=(bm);
00072 fNbench=bm.fNbench;
00073 fNmax=bm.fNmax;
00074
00075 delete [] fNames;
00076 delete [] fRealTime;
00077 delete [] fCpuTime;
00078 delete [] fTimer;
00079
00080 fNames = new TString[fNmax];
00081 fRealTime = new Float_t[fNmax];
00082 fCpuTime = new Float_t[fNmax];
00083 fTimer = new TStopwatch[fNmax];
00084
00085 for(Int_t i = 0; i<fNmax; ++i) {
00086 fNames[i] = bm.fNames[i];
00087 fRealTime[i] = bm.fRealTime[i];
00088 fCpuTime[i] = bm.fCpuTime[i];
00089 fTimer[i] = bm.fTimer[i];
00090 }
00091 }
00092 return *this;
00093 }
00094
00095
00096 TBenchmark::~TBenchmark()
00097 {
00098
00099
00100
00101 fNbench = 0;
00102 if (fNames) { delete [] fNames; fNames = 0;}
00103 if (fRealTime) { delete [] fRealTime; fRealTime = 0;}
00104 if (fCpuTime) { delete [] fCpuTime; fCpuTime = 0;}
00105 if (fTimer ) { delete [] fTimer; fTimer = 0;}
00106 }
00107
00108
00109 Int_t TBenchmark::GetBench(const char *name) const
00110 {
00111
00112
00113
00114 for (Int_t i=0;i<fNbench;i++) {
00115 if (!strcmp(name,(const char*)fNames[i])) return i;
00116 }
00117 return -1;
00118 }
00119
00120
00121 Float_t TBenchmark::GetCpuTime(const char *name)
00122 {
00123
00124
00125
00126 Int_t bench = GetBench(name);
00127 if (bench >= 0) return fCpuTime[bench];
00128 else return 0;
00129 }
00130
00131
00132 Float_t TBenchmark::GetRealTime(const char *name)
00133 {
00134
00135
00136
00137 Int_t bench = GetBench(name);
00138 if (bench >= 0) return fRealTime[bench];
00139 else return 0;
00140 }
00141
00142
00143 void TBenchmark::Print(const char *name) const
00144 {
00145
00146
00147
00148 Int_t bench = GetBench(name);
00149 if (bench < 0) return;
00150 Printf("%-10s: Real Time = %6.2f seconds Cpu Time = %6.2f seconds",name,fRealTime[bench],fCpuTime[bench]);
00151 }
00152
00153
00154 void TBenchmark::Reset()
00155 {
00156
00157
00158
00159 fNbench = 0;
00160
00161 }
00162
00163
00164 void TBenchmark::Show(const char *name)
00165 {
00166
00167
00168
00169 Stop(name);
00170 Print((char*)name);
00171 }
00172
00173
00174 void TBenchmark::Start(const char *name)
00175 {
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 if (!fNames) {
00189 fNames = new TString[fNmax];
00190 fRealTime = new Float_t[fNmax];
00191 fCpuTime = new Float_t[fNmax];
00192 fTimer = new TStopwatch[fNmax];
00193 }
00194 Int_t bench = GetBench(name);
00195 if (bench < 0 && fNbench < fNmax ) {
00196
00197 fNames[fNbench] = name;
00198 bench = fNbench;
00199 fNbench++;
00200 fTimer[bench].Reset();
00201 fTimer[bench].Start();
00202 fRealTime[bench] = 0;
00203 fCpuTime[bench] = 0;
00204 } else if (bench >=0) {
00205
00206 fTimer[bench].Continue();
00207 }
00208 else
00209 Warning("Start","too many benches");
00210
00211 }
00212
00213
00214 void TBenchmark::Stop(const char *name)
00215 {
00216
00217
00218
00219 Int_t bench = GetBench(name);
00220 if (bench < 0) return;
00221
00222 fTimer[bench].Stop();
00223 fRealTime[bench] = fTimer[bench].RealTime();
00224 fCpuTime[bench] = fTimer[bench].CpuTime();
00225
00226 }
00227
00228
00229 void TBenchmark::Summary(Float_t &rt, Float_t &cp)
00230 {
00231
00232
00233
00234 rt = 0;
00235 cp = 0;
00236 for (Int_t i=0;i<fNbench;i++) {
00237 Printf("%-10s: Real Time = %6.2f seconds Cpu Time = %6.2f seconds",(const char*)fNames[i],fRealTime[i],fCpuTime[i]);
00238 rt += fRealTime[i];
00239 cp += fCpuTime[i];
00240 }
00241 Printf("%-10s: Real Time = %6.2f seconds Cpu Time = %6.2f seconds","TOTAL",rt,cp);
00242
00243 }