2 // Author: C. Loizides <mailto:loizides@ikf.physik.uni-frankfurt.de>
5 /** \class AliL3Stopwatch
7 //----------------------------------------------------
10 // Stopwatch class. This class returns the real and cpu time between
11 // the start and stop events (taken from Root)
16 #include "AliL3Stopwatch.h"
20 #include "AliL3StandardIncludes.h"
26 ClassImp(AliL3Stopwatch)
28 clock_t AliL3Stopwatch::gTicks = 0;
30 AliL3Stopwatch::AliL3Stopwatch()
32 // Create a stopwatch and start it.
33 if (!gTicks) gTicks = (clock_t)sysconf(_SC_CLK_TCK);
41 AliL3Stopwatch::~AliL3Stopwatch()
45 void AliL3Stopwatch::Start(Bool_t reset)
47 // Start the stopwatch. If reset is kTRUE reset the stopwatch before
48 // starting it (including the stopwatch counter).
49 // Use kFALSE to continue timing after a Stop() without
50 // resetting the stopwatch.
57 if (fState != kRunning) {
58 fStartRealTime = GetRealTime();
59 fStartCpuTime = GetCPUTime();
65 void AliL3Stopwatch::Stop()
67 // Stop the stopwatch.
68 fStopRealTime = GetRealTime();
69 fStopCpuTime = GetCPUTime();
70 if (fState == kRunning) {
71 fTotalCpuTime += fStopCpuTime - fStartCpuTime;
72 fTotalRealTime += fStopRealTime - fStartRealTime;
77 void AliL3Stopwatch::Continue()
79 // Resume a stopped stopwatch. The stopwatch continues counting from the last
80 // Start() onwards (this is like the laptimer function).
82 if (fState == kUndefined){
83 cerr << "Error in AliL3Stopwatch::Continue! Stopwatch not started." << endl;
87 if (fState == kStopped) {
88 fTotalCpuTime -= fStopCpuTime - fStartCpuTime;
89 fTotalRealTime -= fStopRealTime - fStartRealTime;
95 Double_t AliL3Stopwatch::RealTime()
97 // Return the realtime passed between the start and stop events. If the
98 // stopwatch was still running stop it first.
100 if (fState == kUndefined){
101 cerr << "Error in AliL3Stopwatch::RealTime! Stopwatch not started." << endl;
105 if (fState == kRunning)
108 return fTotalRealTime;
111 Double_t AliL3Stopwatch::CpuTime()
113 // Return the cputime passed between the start and stop events. If the
114 // stopwatch was still running stop it first.
116 if (fState == kUndefined){
117 cerr << "Error in AliL3Stopwatch::CpuTime! Stopwatch not started." << endl;
120 if (fState == kRunning)
123 return fTotalCpuTime;
126 Double_t AliL3Stopwatch::GetRealTime()
129 Double_t trt = (Double_t)times(&cpt);
130 return trt / (Double_t)gTicks;
133 Double_t AliL3Stopwatch::GetCPUTime()
137 return (Double_t)(cpt.tms_utime+cpt.tms_stime) / gTicks;
140 //______________________________________________________________________________
141 void AliL3Stopwatch::Print(Char_t *opt) const
143 // Print the real and cpu time passed between the start and stop events.
144 // and the number of times (slices) this TStopwatch was called
145 // (if this number > 1)
147 Double_t realt = ((AliL3Stopwatch*)(this))->RealTime();
149 Int_t hours = Int_t(realt / 3600);
150 realt -= hours * 3600;
151 Int_t min = Int_t(realt / 60);
153 Int_t sec = Int_t(realt);
154 Int_t counter = Counter();
156 printf("Real time %d:%d:%d, CP time %.3f", hours, min, sec, ((AliL3Stopwatch*)(this))->CpuTime());
158 printf("Real time %d:%d:%d, CP time %.3f, %d slices", hours, min, sec, ((AliL3Stopwatch*)(this))->CpuTime(),counter);