24 # include <sys/times.h>
26 static Double_t gTicks = 0;
29 const Double_t gTicks = 1.0e-7;
39 TStopwatch::TStopwatch()
43 gTicks = (Double_t)sysconf(_SC_CLK_TCK);
58 void TStopwatch::Start(Bool_t reset)
66 if (fState != kRunning) {
67 fStartRealTime = GetRealTime();
68 fStartCpuTime = GetCPUTime();
77 void TStopwatch::Stop()
79 fStopRealTime = GetRealTime();
80 fStopCpuTime = GetCPUTime();
82 if (fState == kRunning) {
83 fTotalCpuTime += fStopCpuTime - fStartCpuTime;
84 fTotalRealTime += fStopRealTime - fStartRealTime;
93 void TStopwatch::Continue()
95 if (fState == kUndefined)
96 Error(
"Continue",
"stopwatch not started");
98 if (fState == kStopped) {
99 fTotalCpuTime -= fStopCpuTime - fStartCpuTime;
100 fTotalRealTime -= fStopRealTime - fStartRealTime;
110 Double_t TStopwatch::RealTime()
112 if (fState == kUndefined)
113 Error(
"RealTime",
"stopwatch not started");
115 if (fState == kRunning)
118 return fTotalRealTime;
125 Double_t TStopwatch::CpuTime()
127 if (fState == kUndefined)
128 Error(
"CpuTime",
"stopwatch not started");
130 if (fState == kRunning)
133 return fTotalCpuTime;
139 Double_t TStopwatch::GetRealTime()
150 SystemTimeToFileTime(&st,&ftRealTime.ftFileTime);
151 return (Double_t)ftRealTime.ftInt64 * gTicks;
158 Double_t TStopwatch::GetCPUTime()
163 return (Double_t)(cpt.tms_utime+cpt.tms_stime) / gTicks;
166 OSVERSIONINFO OsVersionInfo;
174 OsVersionInfo.dwOSVersionInfoSize=
sizeof(OSVERSIONINFO);
175 GetVersionEx(&OsVersionInfo);
176 if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
191 HANDLE hThread = GetCurrentThread();
192 ret = GetThreadTimes (hThread, &ftCreate, &ftExit,
193 &ftKernel.ftFileTime,
196 ret = GetLastError ();
197 ::Error (
"GetCPUTime",
" Error on GetProcessTimes 0x%lx", (
int)ret);
207 return (Double_t) (ftKernel.ftInt64 + ftUser.ftInt64) * gTicks;
209 return GetRealTime();
219 void TStopwatch::Print(Option_t *opt)
const
221 Double_t realt =
const_cast<TStopwatch*
>(
this)->RealTime();
222 Double_t cput =
const_cast<TStopwatch*
>(
this)->CpuTime();
224 Int_t hours = Int_t(realt / 3600);
225 realt -= hours * 3600;
226 Int_t min = Int_t(realt / 60);
228 Int_t sec = Int_t(realt);
230 if (realt < 0) realt = 0;
231 if (cput < 0) cput = 0;
233 if (opt && *opt ==
'm') {
235 Printf(
"Real time %d:%02d:%06.3f, CP time %.3f, %d slices", hours, min, realt, cput, Counter());
237 Printf(
"Real time %d:%02d:%06.3f, CP time %.3f", hours, min, realt, cput);
239 }
else if (opt && *opt ==
'u') {
241 Printf(
"Real time %d:%02d:%09.6f, CP time %.3f, %d slices", hours, min, realt, cput, Counter());
243 Printf(
"Real time %d:%02d:%09.6f, CP time %.3f", hours, min, realt, cput);
247 Printf(
"Real time %d:%02d:%02d, CP time %.3f, %d slices", hours, min, sec, cput, Counter());
249 Printf(
"Real time %d:%02d:%02d, CP time %.3f", hours, min, sec, cput);