--- /dev/null
+// $Id$
+// Author: C. Loizides <mailto:loizides@ikf.physik.uni-frankfurt.de>
+
+
+/** \class AliL3Stopwatch
+//<pre>
+//----------------------------------------------------
+// AliL3Stopwatch
+//
+// Stopwatch class. This class returns the real and cpu time between
+// the start and stop events (taken from Root)
+//</pre>
+*/
+
+#include "AliL3Stopwatch.h"
+
+#ifdef no_root
+#include <stream.h>
+
+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
--- /dev/null
+#ifndef ALIL3_Stopwatch
+#define ALIL3_Stopwatch
+
+#ifndef no_root
+#include <TStopwatch.h>
+typedef TStopwatch AliL3Stopwatch;
+
+#else
+#include <sys/types.h>
+#include <sys/times.h>
+#include <unistd.h>
+#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