36 #ifndef __stir_HIGHRESWALLCLOCKTIMER_H__ 37 #define __stir_HIGHRESWALLCLOCKTIMER_H__ 42 # include <sys/time.h> 86 inline void start(
bool bReset =
false);
88 inline void stop(
void);
90 inline void reset(
void);
99 inline double value(
void);
111 timebasestruct_t m_Start;
112 timebasestruct_t m_Finish;
117 LARGE_INTEGER m_Start;
118 LARGE_INTEGER m_Finish;
121 # define STIR_HRWCT_Use_gettimeofday 151 read_real_time(&m_Start, TIMEBASE_SZ);
153 m_Start = gethrtime();
158 QueryPerformanceCounter(&m_Start);
160 #elif defined(STIR_HRWCT_Use_gettimeofday) 165 gettimeofday(&m_Start, NULL);
181 read_real_time(&m_Finish, TIMEBASE_SZ);
183 time_base_to_time(&m_Start, TIMEBASE_SZ);
184 time_base_to_time(&m_Finish, TIMEBASE_SZ);
186 int Secs = m_Finish.tb_high - m_Start.tb_high;
187 int Nanosecs = m_Finish.tb_low - m_Start.tb_low;
195 Nanosecs += 1000000000;
199 m_Nanosecs += Nanosecs;
200 if (m_Nanosecs >= 1000000000)
203 m_Nanosecs -= 1000000000;
208 m_Finish = gethrtime();
210 m_Secs += (m_Finish - m_Start) / 1000000000;
211 m_Nanosecs += (m_Finish - m_Start) % 1000000000;
212 if (m_Nanosecs >= 1000000000)
215 m_Nanosecs -= 1000000000;
220 BOOL Result = QueryPerformanceCounter(&m_Finish);
223 LONGLONG Delta = m_Finish.QuadPart - m_Start.QuadPart;
225 Result = QueryPerformanceFrequency(&Freq);
227 const auto DeltaSecs = Delta / Freq.QuadPart;
228 m_Secs +=
static_cast<int>(DeltaSecs);
230 Delta -= DeltaSecs * Freq.QuadPart;
233 Delta /= Freq.QuadPart;
234 m_Nanosecs +=
static_cast<int>(Delta);
235 if (m_Nanosecs >= 1000000000)
238 m_Nanosecs -= 1000000000;
241 #elif defined(STIR_HRWCT_Use_gettimeofday) 246 gettimeofday(&m_Finish, NULL);
248 m_Secs += (m_Finish.tv_sec - m_Start.tv_sec);
249 int Microsecs = (m_Finish.tv_usec - m_Start.tv_usec);
253 Microsecs += 1000000;
256 m_Nanosecs += (Microsecs * 1000);
257 if (m_Nanosecs >= 1000000000)
260 m_Nanosecs -= 1000000000;
266 assert(m_Nanosecs >= 0 && m_Nanosecs < 1000000000);
276 m_Secs = m_Nanosecs = 0;
338 timebasestruct_t Start;
339 timebasestruct_t Current;
341 read_real_time(&Start, TIMEBASE_SZ);
342 time_base_to_time(&Start, TIMEBASE_SZ);
345 int MinNanosecs = -1;
346 int SecondsElapsed = 0;
350 timebasestruct_t Times[2];
352 read_real_time(&Times[0], TIMEBASE_SZ);
355 read_real_time(&Times[1], TIMEBASE_SZ);
356 assert(Times[1].flag == Times[0].flag);
357 }
while (Times[1].tb_high == Times[0].tb_high && Times[1].tb_low == Times[0].tb_low);
359 time_base_to_time(&Times[0], TIMEBASE_SZ);
360 time_base_to_time(&Times[1], TIMEBASE_SZ);
362 int Secs = Times[1].tb_high - Times[0].tb_high;
363 int Nanosecs = Times[1].tb_low - Times[0].tb_low;
371 Nanosecs += 1000000000;
374 assert(Secs >= 0 && Nanosecs >= 0 && Nanosecs < 1000000000);
375 assert(Secs != 0 || Nanosecs != 0);
379 if (MinSecs < 0 || Secs < MinSecs || (Secs == MinSecs && Nanosecs < MinNanosecs))
382 MinNanosecs = Nanosecs;
385 read_real_time(&Current, TIMEBASE_SZ);
386 time_base_to_time(&Current, TIMEBASE_SZ);
388 SecondsElapsed = Current.tb_high - Start.tb_high;
389 if (Current.tb_low < Start.tb_low)
391 }
while (SecondsElapsed < 1);
395 Resolution = MinSecs * 1000000000 + MinNanosecs;
399 hrtime_t Start = gethrtime();
402 hrtime_t MinNanosecs = -1;
408 Times[0] = gethrtime();
410 while ((Times[1] = gethrtime()) == Times[0])
413 hrtime_t Nanosecs = Times[1] - Times[0];
415 assert(Nanosecs > 0);
417 if (MinNanosecs < 0 || Nanosecs < MinNanosecs)
419 MinNanosecs = Nanosecs;
422 Current = gethrtime();
423 }
while (Current - Start < 1000000000);
425 assert(MinNanosecs < 2000000000);
427 Resolution =
static_cast<int>(MinNanosecs);
434 Result = QueryPerformanceFrequency(&Freq);
437 Resolution =
static_cast<int>(1000000000 / Freq.QuadPart);
439 #elif defined(STIR_HRWCT_Use_gettimeofday) 445 assert(Resolution > 0);
452 #endif // __HIGHRESWALLCLOCKTIMER_H__ Namespace for the STIR library (and some/most of its applications)
Definition: General_Reconstruction.cxx:6
int get_nanosec(void)
Returns the number of nanoseconds that elapsed on top of whatever get_sec() returned.
Definition: HighResWallClockTimer.h:296
void start(bool bReset=false)
Start a timer.
Definition: HighResWallClockTimer.h:145
double value(void)
Returns the elapsed time (in seconds)
Definition: HighResWallClockTimer.h:304
static int get_resolution_in_nanosecs(void)
Attempts to guess the timer resolution.
Definition: HighResWallClockTimer.h:328
HighResWallClockTimer(void)
Create a timer.
Definition: HighResWallClockTimer.h:128
int get_sec(void)
Returns the number of whole seconds elapsed.
Definition: HighResWallClockTimer.h:288
bool is_running(void)
Check if a timer is running.
Definition: HighResWallClockTimer.h:281
virtual ~HighResWallClockTimer(void)
Destroy a timer.
Definition: HighResWallClockTimer.h:135
High-resolution timer.
Definition: HighResWallClockTimer.h:75
void reset(void)
Reset a timer.
Definition: HighResWallClockTimer.h:273
void stop(void)
Stop a running timer.
Definition: HighResWallClockTimer.h:175