ROOT  6.06/09
Reference Guide
TStopwatch.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Fons Rademakers 11/10/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TStopwatch
13 Stopwatch class. This class returns the real and cpu time between
14 the start and stop events.
15 */
16 
17 #include "TStopwatch.h"
18 #include "TTimeStamp.h"
19 #include "TString.h"
20 
21 #if defined(R__UNIX)
22 # include <sys/times.h>
23 # include <unistd.h>
24 static Double_t gTicks = 0;
25 #elif defined(WIN32)
26 # include "TError.h"
27 const Double_t gTicks = 1.0e-7;
28 # include "Windows4Root.h"
29 #endif
30 
31 
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// Create a stopwatch and start it.
36 
38 {
39 #ifdef R__UNIX
40  if (gTicks <= 0.0)
41  gTicks = (Double_t)sysconf(_SC_CLK_TCK);
42 #endif
43 
44  fStopRealTime = 0;
45  fStopCpuTime = 0;
46 
47  Start();
48 }
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Start the stopwatch. If reset is kTRUE reset the stopwatch before
52 /// starting it (including the stopwatch counter).
53 /// Use kFALSE to continue timing after a Stop() without
54 /// resetting the stopwatch.
55 
57 {
58  if (reset) {
60  fTotalCpuTime = 0;
61  fTotalRealTime = 0;
62  fCounter = 0;
63  }
64  if (fState != kRunning) {
67  }
68  fState = kRunning;
69  fCounter++;
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Stop the stopwatch.
74 
76 {
79 
80  if (fState == kRunning) {
83  }
84  fState = kStopped;
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Resume a stopped stopwatch. The stopwatch continues counting from the last
89 /// Start() onwards (this is like the laptimer function).
90 
92 {
93  if (fState == kUndefined)
94  Error("Continue", "stopwatch not started");
95 
96  if (fState == kStopped) {
99  }
100 
101  fState = kRunning;
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// Stop the stopwatch (if it is running) and return the realtime (in
106 /// seconds) passed between the start and stop events.
107 
109 {
110  if (fState == kUndefined)
111  Error("RealTime", "stopwatch not started");
112 
113  if (fState == kRunning)
114  Stop();
115 
116  return fTotalRealTime;
117 }
118 
119 ////////////////////////////////////////////////////////////////////////////////
120 /// Stop the stopwatch (if it is running) and return the cputime (in
121 /// seconds) passed between the start and stop events.
122 
124 {
125  if (fState == kUndefined)
126  Error("CpuTime", "stopwatch not started");
127 
128  if (fState == kRunning)
129  Stop();
130 
131  return fTotalCpuTime;
132 }
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 /// Private static method returning system realtime.
136 
138 {
139 #if defined(R__UNIX)
140  return TTimeStamp();
141 #elif defined(WIN32)
142  union {
143  FILETIME ftFileTime;
144  __int64 ftInt64;
145  } ftRealTime; // time the process has spent in kernel mode
146  SYSTEMTIME st;
147  GetSystemTime(&st);
148  SystemTimeToFileTime(&st,&ftRealTime.ftFileTime);
149  return (Double_t)ftRealTime.ftInt64 * gTicks;
150 #endif
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Private static method returning system CPU time.
155 
157 {
158 #if defined(R__UNIX)
159  struct tms cpt;
160  times(&cpt);
161  return (Double_t)(cpt.tms_utime+cpt.tms_stime) / gTicks;
162 #elif defined(WIN32)
163 
164  OSVERSIONINFO OsVersionInfo;
165 
166  // Value Platform
167  //----------------------------------------------------
168  // VER_PLATFORM_WIN32s Win32s on Windows 3.1
169  // VER_PLATFORM_WIN32_WINDOWS Win32 on Windows 95
170  // VER_PLATFORM_WIN32_NT Windows NT
171  //
172  OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
173  GetVersionEx(&OsVersionInfo);
174  if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
175  DWORD ret;
176  FILETIME ftCreate, // when the process was created
177  ftExit; // when the process exited
178 
179  union {
180  FILETIME ftFileTime;
181  __int64 ftInt64;
182  } ftKernel; // time the process has spent in kernel mode
183 
184  union {
185  FILETIME ftFileTime;
186  __int64 ftInt64;
187  } ftUser; // time the process has spent in user mode
188 
189  HANDLE hThread = GetCurrentThread();
190  ret = GetThreadTimes (hThread, &ftCreate, &ftExit,
191  &ftKernel.ftFileTime,
192  &ftUser.ftFileTime);
193  if (ret != TRUE) {
194  ret = GetLastError ();
195  ::Error ("GetCPUTime", " Error on GetProcessTimes 0x%lx", (int)ret);
196  }
197 
198  // Process times are returned in a 64-bit structure, as the number of
199  // 100 nanosecond ticks since 1 January 1601. User mode and kernel mode
200  // times for this process are in separate 64-bit structures.
201  // To convert to floating point seconds, we will:
202  //
203  // Convert sum of high 32-bit quantities to 64-bit int
204 
205  return (Double_t) (ftKernel.ftInt64 + ftUser.ftInt64) * gTicks;
206  } else
207  return GetRealTime();
208 #endif
209 }
210 
211 ////////////////////////////////////////////////////////////////////////////////
212 /// Print the real and cpu time passed between the start and stop events.
213 /// and the number of times (slices) this TStopwatch was called
214 /// (if this number > 1). If opt="m" print out realtime in milli second
215 /// precision. If opt="u" print out realtime in micro second precision.
216 
217 void TStopwatch::Print(Option_t *opt) const
218 {
219  Double_t realt = const_cast<TStopwatch*>(this)->RealTime();
220  Double_t cput = const_cast<TStopwatch*>(this)->CpuTime();
221 
222  Int_t hours = Int_t(realt / 3600);
223  realt -= hours * 3600;
224  Int_t min = Int_t(realt / 60);
225  realt -= min * 60;
226  Int_t sec = Int_t(realt);
227 
228  if (realt < 0) realt = 0;
229  if (cput < 0) cput = 0;
230 
231  if (opt && *opt == 'm') {
232  if (Counter() > 1) {
233  Printf("Real time %d:%02d:%06.3f, CP time %.3f, %d slices", hours, min, realt, cput, Counter());
234  } else {
235  Printf("Real time %d:%02d:%06.3f, CP time %.3f", hours, min, realt, cput);
236  }
237  } else if (opt && *opt == 'u') {
238  if (Counter() > 1) {
239  Printf("Real time %d:%02d:%09.6f, CP time %.3f, %d slices", hours, min, realt, cput, Counter());
240  } else {
241  Printf("Real time %d:%02d:%09.6f, CP time %.3f", hours, min, realt, cput);
242  }
243  } else {
244  if (Counter() > 1) {
245  Printf("Real time %d:%02d:%02d, CP time %.3f, %d slices", hours, min, sec, cput, Counter());
246  } else {
247  Printf("Real time %d:%02d:%02d, CP time %.3f", hours, min, sec, cput);
248  }
249  }
250 }
EState fState
Definition: TStopwatch.h:41
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:217
Double_t fTotalRealTime
Definition: TStopwatch.h:40
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:108
const char Option_t
Definition: RtypesCore.h:62
Double_t fStartCpuTime
Definition: TStopwatch.h:37
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:123
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Int_t Counter() const
Definition: TStopwatch.h:52
const Double_t gTicks
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
ClassImp(TStopwatch) TStopwatch
Create a stopwatch and start it.
Definition: TStopwatch.cxx:32
void Continue()
Resume a stopped stopwatch.
Definition: TStopwatch.cxx:91
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
static Double_t GetCPUTime()
Private static method returning system CPU time.
Definition: TStopwatch.cxx:156
Int_t fCounter
Definition: TStopwatch.h:42
static Double_t GetRealTime()
Private static method returning system realtime.
Definition: TStopwatch.cxx:137
#define Printf
Definition: TGeoToOCC.h:18
Double_t fStopRealTime
Definition: TStopwatch.h:36
double Double_t
Definition: RtypesCore.h:55
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition: TTimeStamp.h:76
Double_t fStartRealTime
Definition: TStopwatch.h:35
Double_t fTotalCpuTime
Definition: TStopwatch.h:39
Double_t fStopCpuTime
Definition: TStopwatch.h:38
Stopwatch class.
Definition: TStopwatch.h:30