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