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)
15 #include "AliL3Stopwatch.h"
20 ClassImp(AliL3Stopwatch)
22 clock_t AliL3Stopwatch::gTicks = 0;
24 AliL3Stopwatch::AliL3Stopwatch()
26 // Create a stopwatch and start it.
27 if (!gTicks) gTicks = (clock_t)sysconf(_SC_CLK_TCK);
35 AliL3Stopwatch::~AliL3Stopwatch()
39 void AliL3Stopwatch::Start(Bool_t reset)
41 // Start the stopwatch. If reset is kTRUE reset the stopwatch before
42 // starting it (including the stopwatch counter).
43 // Use kFALSE to continue timing after a Stop() without
44 // resetting the stopwatch.
51 if (fState != kRunning) {
52 fStartRealTime = GetRealTime();
53 fStartCpuTime = GetCPUTime();
59 void AliL3Stopwatch::Stop()
61 // Stop the stopwatch.
62 fStopRealTime = GetRealTime();
63 fStopCpuTime = GetCPUTime();
64 if (fState == kRunning) {
65 fTotalCpuTime += fStopCpuTime - fStartCpuTime;
66 fTotalRealTime += fStopRealTime - fStartRealTime;
71 void AliL3Stopwatch::Continue()
73 // Resume a stopped stopwatch. The stopwatch continues counting from the last
74 // Start() onwards (this is like the laptimer function).
76 if (fState == kUndefined){
77 cerr << "Error in AliL3Stopwatch::Continue! Stopwatch not started." << endl;
81 if (fState == kStopped) {
82 fTotalCpuTime -= fStopCpuTime - fStartCpuTime;
83 fTotalRealTime -= fStopRealTime - fStartRealTime;
89 Double_t AliL3Stopwatch::RealTime()
91 // Return the realtime passed between the start and stop events. If the
92 // stopwatch was still running stop it first.
94 if (fState == kUndefined){
95 cerr << "Error in AliL3Stopwatch::RealTime! Stopwatch not started." << endl;
99 if (fState == kRunning)
102 return fTotalRealTime;
105 Double_t AliL3Stopwatch::CpuTime()
107 // Return the cputime passed between the start and stop events. If the
108 // stopwatch was still running stop it first.
110 if (fState == kUndefined){
111 cerr << "Error in AliL3Stopwatch::CpuTime! Stopwatch not started." << endl;
114 if (fState == kRunning)
117 return fTotalCpuTime;
120 Double_t AliL3Stopwatch::GetRealTime()
123 Double_t trt = (Double_t)times(&cpt);
124 return trt / (Double_t)gTicks;
127 Double_t AliL3Stopwatch::GetCPUTime()
131 return (Double_t)(cpt.tms_utime+cpt.tms_stime) / gTicks;
134 //______________________________________________________________________________
135 void AliL3Stopwatch::Print(Char_t *opt="") const
137 // Print the real and cpu time passed between the start and stop events.
138 // and the number of times (slices) this TStopwatch was called
139 // (if this number > 1)
141 Double_t realt = ((AliL3Stopwatch*)(this))->RealTime();
143 Int_t hours = Int_t(realt / 3600);
144 realt -= hours * 3600;
145 Int_t min = Int_t(realt / 60);
147 Int_t sec = Int_t(realt);
148 Int_t counter = Counter();
150 printf("Real time %d:%d:%d, CP time %.3f", hours, min, sec, ((AliL3Stopwatch*)(this))->CpuTime());
152 printf("Real time %d:%d:%d, CP time %.3f, %d slices", hours, min, sec, ((AliL3Stopwatch*)(this))->CpuTime(),counter);