29 enum EState { kUndefined, kStopped, kRunning };
31 double fStartRealTime;
36 double fTotalRealTime;
40 inline static double GetRealTime();
41 inline static double GetCPUTime();
45 inline void Start(
int reset = 1);
47 inline void Continue();
48 inline int Counter()
const {
return fCounter; }
49 inline double RealTime();
50 inline void Reset() { ResetCpuTime(); ResetRealTime(); }
51 inline void ResetCpuTime(
double time = 0) { Stop(); fTotalCpuTime = time; }
52 inline void ResetRealTime(
double time = 0) { Stop(); fTotalRealTime = time; }
53 inline double CpuTime();
64 # include <sys/time.h>
65 # include <sys/times.h>
67 static double gTicks = 0;
71 static double gTicks = 1000;
73 # include <sys/types.h>
74 # include <sys/timeb.h>
76 # undef min // unfortunately, <windows.h> defines min/max as macros
79 const double gTicks = 1.0e-7;
80 static __int64 gTicksQPC = -1;
85 inline Stopwatch::Stopwatch():
86 fStartRealTime(0), fStopRealTime(0), fStartCpuTime(0), fStopCpuTime(0),
87 fTotalCpuTime(0), fTotalRealTime(0), fState(kUndefined), fCounter(0)
93 gTicks = (double)sysconf(_SC_CLK_TCK);
98 QueryPerformanceFrequency( &freq );
99 gTicksQPC = (double)freq.QuadPart;
107 inline void Stopwatch::Start(
int reset)
120 if (fState != kRunning) {
121 fStartRealTime = GetRealTime();
122 fStartCpuTime = GetCPUTime();
129 inline void Stopwatch::Stop()
133 fStopRealTime = GetRealTime();
134 fStopCpuTime = GetCPUTime();
136 if (fState == kRunning) {
137 fTotalCpuTime += fStopCpuTime - fStartCpuTime;
138 fTotalRealTime += fStopRealTime - fStartRealTime;
144 inline void Stopwatch::Continue()
149 if (fState == kUndefined){
153 if (fState == kStopped) {
154 fTotalCpuTime -= fStopCpuTime - fStartCpuTime;
155 fTotalRealTime -= fStopRealTime - fStartRealTime;
162 inline double Stopwatch::RealTime()
167 if (fState == kUndefined){
171 if (fState == kRunning)
174 return fTotalRealTime;
178 inline double Stopwatch::CpuTime()
183 if (fState == kUndefined){
187 if (fState == kRunning)
190 return fTotalCpuTime;
194 inline double Stopwatch::GetRealTime()
198 gettimeofday(&tp, 0);
199 return tp.tv_sec + (tp.tv_usec)*1.e-6;
200 #elif defined(_WIN32)
201 LARGE_INTEGER counter;
202 QueryPerformanceCounter( &counter );
203 return (
double)counter.QuadPart / gTicksQPC;
210 inline double Stopwatch::GetCPUTime()
217 return (
double)(cpt.tms_utime+cpt.tms_stime) / gTicks;
218 #elif defined(R__VMS)
219 return (
double)clock() / gTicks;
220 #elif defined(_WIN32)
221 OSVERSIONINFO OsVersionInfo;
229 OsVersionInfo.dwOSVersionInfoSize=
sizeof(OSVERSIONINFO);
230 GetVersionEx(&OsVersionInfo);
231 if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
246 HANDLE hProcess = GetCurrentProcess();
247 ret = GetProcessTimes (hProcess, &ftCreate, &ftExit,
248 &ftKernel.ftFileTime,
251 ret = GetLastError ();
263 return (
double) (ftKernel.ftInt64 + ftUser.ftInt64) * gTicks;
265 return GetRealTime();