From: loizides Date: Thu, 11 Jul 2002 10:29:20 +0000 (+0000) Subject: Class for timing measurements, taken from ROOT and made changes to compile it without... X-Git-Url: http://git.uio.no/git/?a=commitdiff_plain;h=4d0891ddeaa598d0e251a1315ffd5a93a389aee9;p=u%2Fmrichter%2FAliRoot.git Class for timing measurements, taken from ROOT and made changes to compile it without ROOT/ALIROOT. --- diff --git a/HLT/misc/AliL3Stopwatch.cxx b/HLT/misc/AliL3Stopwatch.cxx new file mode 100644 index 00000000000..b91157ccc38 --- /dev/null +++ b/HLT/misc/AliL3Stopwatch.cxx @@ -0,0 +1,155 @@ +// $Id$ +// Author: C. Loizides + + +/** \class AliL3Stopwatch +//
+//----------------------------------------------------
+// AliL3Stopwatch
+//
+// Stopwatch class. This class returns the real and cpu time between   
+// the start and stop events (taken from Root)
+//
+*/ + +#include "AliL3Stopwatch.h" + +#ifdef no_root +#include + +ClassImp(AliL3Stopwatch) + +clock_t AliL3Stopwatch::gTicks = 0; + +AliL3Stopwatch::AliL3Stopwatch() +{ + // Create a stopwatch and start it. + if (!gTicks) gTicks = (clock_t)sysconf(_SC_CLK_TCK); + fState = kUndefined; + fTotalCpuTime = 0; + fTotalRealTime = 0; + fCounter = 0; + Start(); +} + +AliL3Stopwatch::~AliL3Stopwatch() +{ +} + +void AliL3Stopwatch::Start(Bool_t reset) +{ + // Start the stopwatch. If reset is kTRUE reset the stopwatch before + // starting it (including the stopwatch counter). + // Use kFALSE to continue timing after a Stop() without + // resetting the stopwatch. + + if (reset) { + fTotalCpuTime = 0; + fTotalRealTime = 0; + fCounter = 0; + } + if (fState != kRunning) { + fStartRealTime = GetRealTime(); + fStartCpuTime = GetCPUTime(); + } + fState = kRunning; + fCounter++; +} + +void AliL3Stopwatch::Stop() +{ + // Stop the stopwatch. + fStopRealTime = GetRealTime(); + fStopCpuTime = GetCPUTime(); + if (fState == kRunning) { + fTotalCpuTime += fStopCpuTime - fStartCpuTime; + fTotalRealTime += fStopRealTime - fStartRealTime; + } + fState = kStopped; +} + +void AliL3Stopwatch::Continue() +{ + // Resume a stopped stopwatch. The stopwatch continues counting from the last + // Start() onwards (this is like the laptimer function). + + if (fState == kUndefined){ + cerr << "Error in AliL3Stopwatch::Continue! Stopwatch not started." << endl; + return; + } + + if (fState == kStopped) { + fTotalCpuTime -= fStopCpuTime - fStartCpuTime; + fTotalRealTime -= fStopRealTime - fStartRealTime; + } + + fState = kRunning; +} + +Double_t AliL3Stopwatch::RealTime() +{ + // Return the realtime passed between the start and stop events. If the + // stopwatch was still running stop it first. + + if (fState == kUndefined){ + cerr << "Error in AliL3Stopwatch::RealTime! Stopwatch not started." << endl; + return -1.0; + } + + if (fState == kRunning) + Stop(); + + return fTotalRealTime; +} + +Double_t AliL3Stopwatch::CpuTime() +{ + // Return the cputime passed between the start and stop events. If the + // stopwatch was still running stop it first. + + if (fState == kUndefined){ + cerr << "Error in AliL3Stopwatch::CpuTime! Stopwatch not started." << endl; + return -1.0; + } + if (fState == kRunning) + Stop(); + + return fTotalCpuTime; +} + +Double_t AliL3Stopwatch::GetRealTime() +{ + struct tms cpt; + Double_t trt = (Double_t)times(&cpt); + return trt / (Double_t)gTicks; +} + +Double_t AliL3Stopwatch::GetCPUTime() +{ + struct tms cpt; + times(&cpt); + return (Double_t)(cpt.tms_utime+cpt.tms_stime) / gTicks; +} + +//______________________________________________________________________________ +void AliL3Stopwatch::Print(Char_t *opt="") const +{ + // Print the real and cpu time passed between the start and stop events. + // and the number of times (slices) this TStopwatch was called + // (if this number > 1) + + Double_t realt = ((AliL3Stopwatch*)(this))->RealTime(); + + Int_t hours = Int_t(realt / 3600); + realt -= hours * 3600; + Int_t min = Int_t(realt / 60); + realt -= min * 60; + Int_t sec = Int_t(realt); + Int_t counter = Counter(); + if (counter <= 1 ) + printf("Real time %d:%d:%d, CP time %.3f", hours, min, sec, ((AliL3Stopwatch*)(this))->CpuTime()); + else + printf("Real time %d:%d:%d, CP time %.3f, %d slices", hours, min, sec, ((AliL3Stopwatch*)(this))->CpuTime(),counter); +} + +#endif diff --git a/HLT/misc/AliL3Stopwatch.h b/HLT/misc/AliL3Stopwatch.h new file mode 100644 index 00000000000..b4dc7feaeea --- /dev/null +++ b/HLT/misc/AliL3Stopwatch.h @@ -0,0 +1,51 @@ +#ifndef ALIL3_Stopwatch +#define ALIL3_Stopwatch + +#ifndef no_root +#include +typedef TStopwatch AliL3Stopwatch; + +#else +#include +#include +#include +#include "AliL3RootTypes.h" + +class AliL3Stopwatch +{ + private: + static clock_t gTicks; + enum EState { kUndefined, kStopped, kRunning }; + + Double_t fStartRealTime; //wall clock start time + Double_t fStopRealTime; //wall clock stop time + Double_t fStartCpuTime; //cpu start time + Double_t fStopCpuTime; //cpu stop time + Double_t fTotalCpuTime; //total cpu time + Double_t fTotalRealTime; //total real time + EState fState; //stopwatch state + Int_t fCounter; //number of times the stopwatch was started + + public: + + AliL3Stopwatch(); + ~AliL3Stopwatch(); + void Start(Bool_t reset = kTRUE); + void Stop(); + void Continue(); + Int_t Counter() const { return fCounter; } + Double_t RealTime(); + void Reset() { ResetCpuTime(); ResetRealTime(); } + void ResetCpuTime (Double_t time = 0) { Stop(); fTotalCpuTime = time; } + void ResetRealTime(Double_t time = 0) { Stop(); fTotalRealTime = time; } + Double_t CpuTime(); + void Print(Char_t *opt="") const; + static Double_t GetRealTime(); + static Double_t GetCPUTime(); + + ClassDef(TStopwatch,1) //A stopwatch which times real and cpu time +}; + +#endif + +#endif