TBenchmark.cxx

Go to the documentation of this file.
00001 
00002 /*************************************************************************
00003  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00004  * All rights reserved.                                                  *
00005  *                                                                       *
00006  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00007  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
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 // This class is a ROOT utility to help benchmarking applications
00023 //
00024 //  Examples of use of this class are given in the tutorials macros.
00025 //////////////////////////////////////////////////////////////////////////
00026 
00027 
00028 //______________________________________________________________________________
00029 TBenchmark::TBenchmark(): TNamed()
00030 {
00031 //*-*-*-*-*-*-*-*-*-*-*Benchmark default constructor*-*-*-*-*-*-*-*-*-*-*-*-*-*
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    //copy constructor
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    //assignment operator
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 //*-*-*-*-*-*-*-*-*-*-*Benchmark default destructor*-*-*-*-*-*-*-*-*-*-*-*-*-*
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 //*-*-*-*-*-*-*-*-*-*-*Returns index of Benchmark name*-*-*-*-*-*-*-*
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 //*-*-*-*-*-*-*-*-*-*-*Returns Cpu time used by Benchmark name*-*-*-*-*-*-*-*
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 //*-*-*-*-*-*-*-*-*-*-*Returns Realtime used by Benchmark name*-*-*-*-*-*-*-*
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 //*-*-*-*-*-*-*-*-*-*-*Prints parameters of Benchmark name*-*-*-*-*-*-*-*-*-*
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 //*-*-*-*-*-*-*-*-*-*-*-*-*-*Reset all Benchmarks*-*-*-*-*-*-*-*-*-*-*-*-*
00157 //*-*                        ====================
00158 
00159    fNbench = 0;
00160 
00161 }
00162 
00163 //______________________________________________________________________________
00164 void TBenchmark::Show(const char *name)
00165 {
00166 //*-*-*-*-*-*-*-*-*Stops Benchmark name and Prints results*-*-*-*-*-*-*-*-*-*
00167 //*-*              =======================================
00168 
00169    Stop(name);
00170    Print((char*)name);
00171 }
00172 
00173 //______________________________________________________________________________
00174 void TBenchmark::Start(const char *name)
00175 {
00176 //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*Starts Benchmark name*-*-*-*-*-*-*-*-*-*
00177 //*-*                          =====================
00178 //*-*
00179 //*-*   An independent timer (see class TStopwatch) is started.
00180 //*-*   the name of the benchmark is entered into the list of benchmarks.
00181 //*-*   Benchmark can be stopped via TBenchmark::Stop
00182 //*-*   Results can be printed via TBenchmark::Print
00183 //*-*   TBenchmark::Show can be used to stop benchmark and print results.
00184 //*-*   If name is an already existing benchmark, existing parameters are reset.
00185 //*-*   A summary of all benchmarks can be seen via TBenchmark::Summary.
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    // define a new benchmark to Start
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    // Resume the existen benchmark
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 //*-*-*-*-*-*-*-*-*-*-*Terminates Benchmark name*-*-*-*-*-*-*-*-*-*-*-*
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 //*-*-*-*-*-*-*-*-*-*-*-*-*-*Prints a summary of all benchmarks*-*-*-*-*-*-*-*
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 }

Generated on Tue Jul 5 14:11:19 2011 for ROOT_528-00b_version by  doxygen 1.5.1