1 // Copyright (c) 2005-2010 Hartmut Kaiser
2 // Copyright (c) 2009 Edward Grace
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #if !defined(HIGH_RESOLUTION_TIMER_MAR_24_2008_1222PM)
8 #define HIGH_RESOLUTION_TIMER_MAR_24_2008_1222PM
10 #include <boost/config.hpp>
11 #include <boost/throw_exception.hpp>
13 #if defined(BOOST_HAS_UNISTD_H)
20 #if defined(BOOST_WINDOWS)
26 ///////////////////////////////////////////////////////////////////////////////
28 // high_resolution_timer
29 // A timer object measures elapsed time.
30 // CAUTION: Windows only!
32 ///////////////////////////////////////////////////////////////////////////////
33 class high_resolution_timer
36 high_resolution_timer()
41 high_resolution_timer(double t)
43 LARGE_INTEGER frequency;
44 if (!QueryPerformanceFrequency(&frequency))
45 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
47 start_time.QuadPart = (LONGLONG)(t * frequency.QuadPart);
50 high_resolution_timer(high_resolution_timer const& rhs)
51 : start_time(rhs.start_time)
61 SystemTimeToFileTime(&st, &ft);
64 now.LowPart = ft.dwLowDateTime;
65 now.HighPart = ft.dwHighDateTime;
67 // FileTime is in 100ns increments, result needs to be in [s]
68 return now.QuadPart * 1e-7;
73 if (!QueryPerformanceCounter(&start_time))
74 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
76 double elapsed() const // return elapsed time in seconds
79 if (!QueryPerformanceCounter(&now))
80 boost::throw_exception(std::runtime_error("Couldn't get current time"));
82 LARGE_INTEGER frequency;
83 if (!QueryPerformanceFrequency(&frequency))
84 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
86 return double(now.QuadPart - start_time.QuadPart) / frequency.QuadPart;
89 double elapsed_max() const // return estimated maximum value for elapsed()
91 LARGE_INTEGER frequency;
92 if (!QueryPerformanceFrequency(&frequency))
93 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
95 return double((std::numeric_limits<LONGLONG>::max)() - start_time.QuadPart) /
96 double(frequency.QuadPart);
99 double elapsed_min() const // return minimum value for elapsed()
101 LARGE_INTEGER frequency;
102 if (!QueryPerformanceFrequency(&frequency))
103 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
105 return 1.0 / frequency.QuadPart;
109 LARGE_INTEGER start_time;
114 #elif defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(_POSIX_THREAD_CPUTIME)
116 #if _POSIX_THREAD_CPUTIME > 0 // timer always supported
121 ///////////////////////////////////////////////////////////////////////////////
123 // high_resolution_timer
124 // A timer object measures elapsed time.
126 ///////////////////////////////////////////////////////////////////////////////
127 class high_resolution_timer
130 high_resolution_timer()
132 start_time.tv_sec = 0;
133 start_time.tv_nsec = 0;
138 high_resolution_timer(double t)
140 start_time.tv_sec = time_t(t);
141 start_time.tv_nsec = (t - start_time.tv_sec) * 1e9;
144 high_resolution_timer(high_resolution_timer const& rhs)
145 : start_time(rhs.start_time)
152 if (-1 == clock_gettime(CLOCK_REALTIME, &now))
153 boost::throw_exception(std::runtime_error("Couldn't get current time"));
154 return double(now.tv_sec) + double(now.tv_nsec) * 1e-9;
159 if (-1 == clock_gettime(CLOCK_REALTIME, &start_time))
160 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
162 double elapsed() const // return elapsed time in seconds
165 if (-1 == clock_gettime(CLOCK_REALTIME, &now))
166 boost::throw_exception(std::runtime_error("Couldn't get current time"));
168 if (now.tv_sec == start_time.tv_sec)
169 return double(now.tv_nsec - start_time.tv_nsec) * 1e-9;
171 return double(now.tv_sec - start_time.tv_sec) +
172 (double(now.tv_nsec - start_time.tv_nsec) * 1e-9);
175 double elapsed_max() const // return estimated maximum value for elapsed()
177 return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
180 double elapsed_min() const // return minimum value for elapsed()
183 if (-1 == clock_getres(CLOCK_REALTIME, &resolution))
184 boost::throw_exception(std::runtime_error("Couldn't get resolution"));
185 return double(resolution.tv_sec + resolution.tv_nsec * 1e-9);
194 #else // _POSIX_THREAD_CPUTIME > 0
196 #include <boost/timer.hpp>
198 // availability of high performance timers must be checked at runtime
201 ///////////////////////////////////////////////////////////////////////////////
203 // high_resolution_timer
204 // A timer object measures elapsed time.
206 ///////////////////////////////////////////////////////////////////////////////
207 class high_resolution_timer
210 high_resolution_timer()
211 : use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0)
214 start_time.tv_sec = 0;
215 start_time.tv_nsec = 0;
220 high_resolution_timer(double t)
221 : use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0)
224 start_time.tv_sec = time_t(t);
225 start_time.tv_nsec = (t - start_time.tv_sec) * 1e9;
229 high_resolution_timer(high_resolution_timer const& rhs)
230 : use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0),
231 start_time(rhs.start_time)
237 if (sysconf(_SC_THREAD_CPUTIME) <= 0)
238 return double(std::clock());
241 if (-1 == clock_gettime(CLOCK_REALTIME, &now))
242 boost::throw_exception(std::runtime_error("Couldn't get current time"));
243 return double(now.tv_sec) + double(now.tv_nsec) * 1e-9;
249 start_time_backup.restart();
250 else if (-1 == clock_gettime(CLOCK_REALTIME, &start_time))
251 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
253 double elapsed() const // return elapsed time in seconds
256 return start_time_backup.elapsed();
259 if (-1 == clock_gettime(CLOCK_REALTIME, &now))
260 boost::throw_exception(std::runtime_error("Couldn't get current time"));
262 if (now.tv_sec == start_time.tv_sec)
263 return double(now.tv_nsec - start_time.tv_nsec) * 1e-9;
265 return double(now.tv_sec - start_time.tv_sec) +
266 (double(now.tv_nsec - start_time.tv_nsec) * 1e-9);
269 double elapsed_max() const // return estimated maximum value for elapsed()
272 start_time_backup.elapsed_max();
274 return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
277 double elapsed_min() const // return minimum value for elapsed()
280 start_time_backup.elapsed_min();
283 if (-1 == clock_getres(CLOCK_REALTIME, &resolution))
284 boost::throw_exception(std::runtime_error("Couldn't get resolution"));
285 return double(resolution.tv_sec + resolution.tv_nsec * 1e-9);
291 boost::timer start_time_backup;
296 #endif // _POSIX_THREAD_CPUTIME > 0
298 #else // !defined(BOOST_WINDOWS) && (!defined(_POSIX_TIMERS)
299 // || _POSIX_TIMERS <= 0
300 // || !defined(_POSIX_THREAD_CPUTIME)
301 // || _POSIX_THREAD_CPUTIME <= 0)
303 #if defined(BOOST_HAS_GETTIMEOFDAY)
305 // For platforms that do not support _POSIX_TIMERS but do have
306 // GETTIMEOFDAY, which is still preferable to std::clock()
307 #include <sys/time.h>
312 ///////////////////////////////////////////////////////////////////////////
314 // high_resolution_timer
315 // A timer object measures elapsed time.
317 // Implemented with gettimeofday() for platforms that support it,
318 // such as Darwin (OS X) but do not support the previous options.
320 // Copyright (c) 2009 Edward Grace
322 ///////////////////////////////////////////////////////////////////////////
323 class high_resolution_timer
326 template <typename U>
327 static inline double unsigned_diff(const U &a, const U &b)
330 return static_cast<double>(a-b);
331 return -static_cast<double>(b-a);
334 // @brief Return the difference between two timeval types.
336 // @param t1 The most recent timeval.
337 // @param t0 The historical timeval.
339 // @return The difference between the two in seconds.
340 double elapsed(const timeval &t1, const timeval &t0) const
342 if (t1.tv_sec == t0.tv_sec)
343 return unsigned_diff(t1.tv_usec,t0.tv_usec) * 1e-6;
345 // We do it this way as the result of the difference of the
346 // microseconds can be negative if the clock is implemented so
347 // that the seconds timer increases in large steps.
349 // Naive subtraction of the unsigned types and conversion to
350 // double can wreak havoc!
351 return unsigned_diff(t1.tv_sec,t0.tv_sec) +
352 unsigned_diff(t1.tv_usec,t0.tv_usec) * 1e-6;
356 high_resolution_timer()
358 start_time.tv_sec = 0;
359 start_time.tv_usec = 0;
364 high_resolution_timer(double t)
366 start_time.tv_sec = time_t(t);
367 start_time.tv_usec = (t - start_time.tv_sec) * 1e6;
370 high_resolution_timer(high_resolution_timer const& rhs)
371 : start_time(rhs.start_time)
377 // Under some implementations gettimeofday() will always
378 // return zero. If it returns anything else however then
379 // we accept this as evidence of an error. Note we are
380 // not assuming that -1 explicitly indicates the error
381 // condition, just that non zero is indicative of the
384 if (gettimeofday(&now, NULL))
385 boost::throw_exception(std::runtime_error("Couldn't get current time"));
386 return double(now.tv_sec) + double(now.tv_usec) * 1e-6;
391 if (gettimeofday(&start_time, NULL))
392 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
395 double elapsed() const // return elapsed time in seconds
398 if (gettimeofday(&now, NULL))
399 boost::throw_exception(std::runtime_error("Couldn't get current time"));
400 return elapsed(now,start_time);
403 double elapsed_max() const // return estimated maximum value for elapsed()
405 return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
408 double elapsed_min() const // return minimum value for elapsed()
410 // On systems without an explicit clock_getres or similar
411 // we can only estimate an upper bound on the resolution
412 // by repeatedly calling the gettimeofday function. This
413 // is often likely to be indicative of the true
418 if (gettimeofday(&t0, NULL))
419 boost::throw_exception(std::runtime_error("Couldn't get resolution."));
421 // Spin around in a tight loop until we observe a change
422 // in the reported timer value.
424 if (gettimeofday(&t1, NULL))
425 boost::throw_exception(std::runtime_error("Couldn't get resolution."));
426 delta = elapsed(t1, t0);
427 } while (delta <= 0.0);
438 #else // BOOST_HAS_GETTIMEOFDAY
440 // For platforms other than Windows or Linux, or not implementing gettimeofday
441 // simply fall back to boost::timer
442 #include <boost/timer.hpp>
446 struct high_resolution_timer
451 return double(std::clock());
460 #endif // HIGH_RESOLUTION_TIMER_AUG_14_2009_0425PM
463 // $Log: high_resolution_timer.hpp,v $
464 // Revision 1.4 2009/08/14 15:28:10 graceej
465 // * It is entirely possible for the updating clock to increment the
466 // * seconds and *decrement* the microseconds field. Consequently
467 // * when subtracting these unsigned microseconds fields a wrap-around
468 // * error can occur. For this reason elapsed(t1, t0) is used in a
469 // * similar maner to cycle.h this preserves the sign of the