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
34
35////////////////////////////////////////////////////////////////////////////////
36/// Create a stopwatch and start it.
37
39{
40#ifdef R__UNIX
41 if (gTicks <= 0.0)
43#endif
44
45 fStopRealTime = 0;
46 fStopCpuTime = 0;
47
48 Start();
49}
50
51////////////////////////////////////////////////////////////////////////////////
52/// Start the stopwatch. If reset is kTRUE reset the stopwatch before
53/// starting it (including the stopwatch counter).
54/// Use kFALSE to continue timing after a Stop() without
55/// resetting the stopwatch.
56
58{
59 if (reset) {
61 fTotalCpuTime = 0;
63 fCounter = 0;
64 }
65 if (fState != kRunning) {
68 }
70 fCounter++;
71}
72
73////////////////////////////////////////////////////////////////////////////////
74/// Stop the stopwatch.
75
87
88////////////////////////////////////////////////////////////////////////////////
89/// Resume a stopped stopwatch. The stopwatch continues counting from the last
90/// Start() onwards (this is like the laptimer function).
91
93{
94 if (fState == kUndefined)
95 Error("Continue", "stopwatch not started");
96
97 if (fState == kStopped) {
100 }
101
103}
104
105////////////////////////////////////////////////////////////////////////////////
106/// Stop the stopwatch (if it is running) and return the realtime (in
107/// seconds) passed between the start and stop events.
108
110{
111 if (fState == kUndefined)
112 Error("RealTime", "stopwatch not started");
113
114 if (fState == kRunning)
115 Stop();
116
117 return fTotalRealTime;
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// Stop the stopwatch (if it is running) and return the cputime (in
122/// seconds) passed between the start and stop events.
123
125{
126 if (fState == kUndefined)
127 Error("CpuTime", "stopwatch not started");
128
129 if (fState == kRunning)
130 Stop();
131
132 return fTotalCpuTime;
133}
134
135////////////////////////////////////////////////////////////////////////////////
136/// Private static method returning system realtime.
137
139{
140#if defined(R__UNIX)
141 return TTimeStamp();
142#elif defined(WIN32)
143 union {
146 } ftRealTime; // time the process has spent in kernel mode
148 GetSystemTime(&st);
149 SystemTimeToFileTime(&st,&ftRealTime.ftFileTime);
150 return (Double_t)ftRealTime.ftInt64 * gTicks;
151#endif
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Private static method returning system CPU time.
156
158{
159#if defined(R__UNIX)
160 struct tms cpt;
161 times(&cpt);
162 return (Double_t)(cpt.tms_utime+cpt.tms_stime) / gTicks;
163#elif defined(WIN32)
164
166
167 // Value Platform
168 //----------------------------------------------------
169 // VER_PLATFORM_WIN32s Win32s on Windows 3.1
170 // VER_PLATFORM_WIN32_WINDOWS Win32 on Windows 95
171 // VER_PLATFORM_WIN32_NT Windows NT
172 //
173 OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
175 if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
176 DWORD ret;
177 FILETIME ftCreate, // when the process was created
178 ftExit; // when the process exited
179
180 union {
183 } ftKernel; // time the process has spent in kernel mode
184
185 union {
188 } ftUser; // time the process has spent in user mode
189
190 HANDLE hThread = GetCurrentThread();
192 &ftKernel.ftFileTime,
193 &ftUser.ftFileTime);
194 if (ret != TRUE) {
195 ret = GetLastError ();
196 ::Error ("GetCPUTime", " Error on GetProcessTimes 0x%lx", (int)ret);
197 }
198
199 // Process times are returned in a 64-bit structure, as the number of
200 // 100 nanosecond ticks since 1 January 1601. User mode and kernel mode
201 // times for this process are in separate 64-bit structures.
202 // To convert to floating point seconds, we will:
203 //
204 // Convert sum of high 32-bit quantities to 64-bit int
205
206 return (Double_t) (ftKernel.ftInt64 + ftUser.ftInt64) * gTicks;
207 } else
208 return GetRealTime();
209#endif
210}
211
212////////////////////////////////////////////////////////////////////////////////
213/// Print the real and cpu time passed between the start and stop events.
214/// and the number of times (slices) this TStopwatch was called
215/// (if this number > 1). If opt="m" print out realtime in milli second
216/// precision. If opt="u" print out realtime in micro second precision.
217
219{
220 Double_t realt = const_cast<TStopwatch*>(this)->RealTime();
221 Double_t cput = const_cast<TStopwatch*>(this)->CpuTime();
222
223 Int_t hours = Int_t(realt / 3600);
224 realt -= hours * 3600;
225 Int_t min = Int_t(realt / 60);
226 realt -= min * 60;
227 Int_t sec = Int_t(realt);
228
229 if (realt < 0) realt = 0;
230 if (cput < 0) cput = 0;
231
232 if (opt && *opt == 'm') {
233 if (Counter() > 1) {
234 Printf("Real time %d:%02d:%06.3f, CP time %.3f, %d slices", hours, min, realt, cput, Counter());
235 } else {
236 Printf("Real time %d:%02d:%06.3f, CP time %.3f", hours, min, realt, cput);
237 }
238 } else if (opt && *opt == 'u') {
239 if (Counter() > 1) {
240 Printf("Real time %d:%02d:%09.6f, CP time %.3f, %d slices", hours, min, realt, cput, Counter());
241 } else {
242 Printf("Real time %d:%02d:%09.6f, CP time %.3f", hours, min, realt, cput);
243 }
244 } else {
245 if (Counter() > 1) {
246 Printf("Real time %d:%02d:%02d, CP time %.3f, %d slices", hours, min, sec, cput, Counter());
247 } else {
248 Printf("Real time %d:%02d:%02d, CP time %.3f", hours, min, sec, cput);
249 }
250 }
251}
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2509
const Double_t gTicks
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
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.
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
void Print(Option_t *option="") const override
Print the real and cpu time passed between the start and stop events.
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition TTimeStamp.h:45