Ticket #3345: high_resolution_timer.hpp

File high_resolution_timer.hpp, 13.3 KB (added by ej.grace@…, 13 years ago)

A patched version of high_resolution_timer.hpp that includes support for gettimeofday() if it exists in preference to boost::timer().

Line 
1// Copyright (c) 2005-2009 Hartmut Kaiser
2// Copyright (c) 2009 Edward Grace
3//
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)
6
7#if !defined(HIGH_RESOLUTION_TIMER_AUG_14_2009_1108AM)
8#define HIGH_RESOLUTION_TIMER_AUG_14_2009_1108AM
9
10#include <boost/config.hpp>
11#include <boost/throw_exception.hpp>
12
13#if defined(BOOST_HAS_UNISTD_H)
14#include <unistd.h>
15#endif
16#include <time.h>
17
18#if defined(BOOST_WINDOWS)
19
20#include <stdexcept>
21#include <limits>
22#include <windows.h>
23
24namespace util
25{
26 ///////////////////////////////////////////////////////////////////////////////
27 //
28 // high_resolution_timer
29 // A timer object measures elapsed time.
30 // CAUTION: Windows only!
31 //
32 ///////////////////////////////////////////////////////////////////////////////
33 class high_resolution_timer
34 {
35 public:
36 high_resolution_timer()
37 {
38 restart();
39 }
40
41 high_resolution_timer(double t)
42 {
43 LARGE_INTEGER frequency;
44 if (!QueryPerformanceFrequency(&frequency))
45 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
46
47 start_time.QuadPart = (LONGLONG)(t * frequency.QuadPart);
48 }
49
50 high_resolution_timer(high_resolution_timer const& rhs)
51 : start_time(rhs.start_time)
52 {
53 }
54
55 static double now()
56 {
57 SYSTEMTIME st;
58 GetSystemTime(&st);
59
60 FILETIME ft;
61 SystemTimeToFileTime(&st, &ft);
62
63 LARGE_INTEGER now;
64 now.LowPart = ft.dwLowDateTime;
65 now.HighPart = ft.dwHighDateTime;
66
67 // FileTime is in 100ns increments, result needs to be in [s]
68 return now.QuadPart * 1e-7;
69 }
70
71 void restart()
72 {
73 if (!QueryPerformanceCounter(&start_time))
74 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
75 }
76 double elapsed() const // return elapsed time in seconds
77 {
78 LARGE_INTEGER now;
79 if (!QueryPerformanceCounter(&now))
80 boost::throw_exception(std::runtime_error("Couldn't get current time"));
81
82 LARGE_INTEGER frequency;
83 if (!QueryPerformanceFrequency(&frequency))
84 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
85
86 return double(now.QuadPart - start_time.QuadPart) / frequency.QuadPart;
87 }
88
89 double elapsed_max() const // return estimated maximum value for elapsed()
90 {
91 LARGE_INTEGER frequency;
92 if (!QueryPerformanceFrequency(&frequency))
93 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
94
95 return double((std::numeric_limits<LONGLONG>::max)() - start_time.QuadPart) /
96 double(frequency.QuadPart);
97 }
98
99 double elapsed_min() const // return minimum value for elapsed()
100 {
101 LARGE_INTEGER frequency;
102 if (!QueryPerformanceFrequency(&frequency))
103 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
104
105 return 1.0 / frequency.QuadPart;
106 }
107
108 private:
109 LARGE_INTEGER start_time;
110 };
111
112} // namespace util
113
114#elif defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(_POSIX_THREAD_CPUTIME)
115
116#if _POSIX_THREAD_CPUTIME > 0 // timer always supported
117
118namespace util
119{
120
121 ///////////////////////////////////////////////////////////////////////////////
122 //
123 // high_resolution_timer
124 // A timer object measures elapsed time.
125 //
126 ///////////////////////////////////////////////////////////////////////////////
127 class high_resolution_timer
128 {
129 public:
130 high_resolution_timer()
131 {
132 start_time.tv_sec = 0;
133 start_time.tv_nsec = 0;
134
135 restart();
136 }
137
138 high_resolution_timer(double t)
139 {
140 start_time.tv_sec = time_t(t);
141 start_time.tv_nsec = (t - start_time.tv_sec) * 1e9;
142 }
143
144 high_resolution_timer(high_resolution_timer const& rhs)
145 : start_time(rhs.start_time)
146 {
147 }
148
149 static double now()
150 {
151 timespec now;
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;
155 }
156
157 void restart()
158 {
159 if (-1 == clock_gettime(CLOCK_REALTIME, &start_time))
160 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
161 }
162 double elapsed() const // return elapsed time in seconds
163 {
164 timespec now;
165 if (-1 == clock_gettime(CLOCK_REALTIME, &now))
166 boost::throw_exception(std::runtime_error("Couldn't get current time"));
167
168 if (now.tv_sec == start_time.tv_sec)
169 return double(now.tv_nsec - start_time.tv_nsec) * 1e-9;
170
171 return double(now.tv_sec - start_time.tv_sec) +
172 (double(now.tv_nsec - start_time.tv_nsec) * 1e-9);
173 }
174
175 double elapsed_max() const // return estimated maximum value for elapsed()
176 {
177 return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
178 }
179
180 double elapsed_min() const // return minimum value for elapsed()
181 {
182 timespec resolution;
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);
186 }
187
188 private:
189 timespec start_time;
190 };
191
192} // namespace util
193
194#else // _POSIX_THREAD_CPUTIME > 0
195
196#include <boost/timer.hpp>
197
198// availability of high performance timers must be checked at runtime
199namespace util
200{
201 ///////////////////////////////////////////////////////////////////////////////
202 //
203 // high_resolution_timer
204 // A timer object measures elapsed time.
205 //
206 ///////////////////////////////////////////////////////////////////////////////
207 class high_resolution_timer
208 {
209 public:
210 high_resolution_timer()
211 : use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0)
212 {
213 if (!use_backup) {
214 start_time.tv_sec = 0;
215 start_time.tv_nsec = 0;
216 }
217 restart();
218 }
219
220 high_resolution_timer(double t)
221 : use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0)
222 {
223 if (!use_backup) {
224 start_time.tv_sec = time_t(t);
225 start_time.tv_nsec = (t - start_time.tv_sec) * 1e9;
226 }
227 }
228
229 high_resolution_timer(high_resolution_timer const& rhs)
230 : use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0),
231 start_time(rhs.start_time)
232 {
233 }
234
235 static double now()
236 {
237 if (sysconf(_SC_THREAD_CPUTIME) <= 0)
238 return double(std::clock());
239
240 timespec now;
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;
244 }
245
246 void restart()
247 {
248 if (use_backup)
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"));
252 }
253 double elapsed() const // return elapsed time in seconds
254 {
255 if (use_backup)
256 return start_time_backup.elapsed();
257
258 timespec now;
259 if (-1 == clock_gettime(CLOCK_REALTIME, &now))
260 boost::throw_exception(std::runtime_error("Couldn't get current time"));
261
262 if (now.tv_sec == start_time.tv_sec)
263 return double(now.tv_nsec - start_time.tv_nsec) * 1e-9;
264
265 return double(now.tv_sec - start_time.tv_sec) +
266 (double(now.tv_nsec - start_time.tv_nsec) * 1e-9);
267 }
268
269 double elapsed_max() const // return estimated maximum value for elapsed()
270 {
271 if (use_backup)
272 start_time_backup.elapsed_max();
273
274 return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
275 }
276
277 double elapsed_min() const // return minimum value for elapsed()
278 {
279 if (use_backup)
280 start_time_backup.elapsed_min();
281
282 timespec resolution;
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);
286 }
287
288 private:
289 bool use_backup;
290 timespec start_time;
291 boost::timer start_time_backup;
292 };
293
294} // namespace util
295
296#endif // _POSIX_THREAD_CPUTIME > 0
297
298
299#else // !defined(BOOST_WINDOWS) && (!defined(_POSIX_TIMERS)
300 // || _POSIX_TIMERS <= 0
301 // || !defined(_POSIX_THREAD_CPUTIME)
302 // || _POSIX_THREAD_CPUTIME <= 0)
303
304#if defined(BOOST_HAS_GETTIMEOFDAY)
305
306// For platforms that do not support _POSIX_TIMERS but do have
307// GETTIMEOFDAY, which is still preferable to std::clock()
308#include <stdexcept>
309#include <limits>
310
311namespace util
312{
313
314 ///////////////////////////////////////////////////////////////////////////////
315 //
316 // high_resolution_timer
317 // A timer object measures elapsed time.
318 //
319 // Implemented with gettimeofday() for platforms that support it,
320 // such as Darwin (OS X) but do not support the previous options.
321 //
322 // Copyright (c) 2009 Edward Grace
323 //
324 ///////////////////////////////////////////////////////////////////////////////
325 class high_resolution_timer
326 {
327 public:
328 high_resolution_timer()
329 {
330 start_time.tv_sec = 0;
331 start_time.tv_usec = 0;
332
333 restart();
334 }
335
336 high_resolution_timer(double t)
337 {
338 start_time.tv_sec = time_t(t);
339 start_time.tv_usec = (t - start_time.tv_sec) * 1e6;
340 }
341
342 high_resolution_timer(high_resolution_timer const& rhs)
343 : start_time(rhs.start_time)
344 {
345 }
346
347 static double now()
348 {
349 timeval now;
350 // Under some implementations gettimeofday() will always
351 // return zero. If it returns anything else however then
352 // we accept this as evidence of an error. Note we are
353 // not assuming that -1 explicitly indicates the error
354 // condition, just that non zero is indicative of the
355 // error.
356 if (gettimeofday(&now,NULL))
357 boost::throw_exception(std::runtime_error("Couldn't get current time"));
358 return double(now.tv_sec) + double(now.tv_usec) * 1e-6;
359 }
360
361 void restart()
362 {
363 if (gettimeofday(&start_time,NULL))
364 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
365 }
366
367 double elapsed() const // return elapsed time in seconds
368 {
369 timeval now;
370 if (gettimeofday(&now,NULL))
371 boost::throw_exception(std::runtime_error("Couldn't get current time"));
372
373 if (now.tv_sec == start_time.tv_sec)
374 return double(now.tv_usec - start_time.tv_usec) * 1e-6;
375
376 return double(now.tv_usec - start_time.tv_sec) +
377 (double(now.tv_usec - start_time.tv_usec) * 1e-6);
378 }
379
380 double elapsed_max() const // return estimated maximum value for elapsed()
381 {
382 return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
383 }
384
385 double elapsed_min() const // return minimum value for elapsed()
386 {
387 timeval a,b;
388 double delta(0);
389 // On systems without an explicit clock_getres or similar
390 // we can only estimate an upper bound on the resolution
391 // by repeatedly calling the gettimeofday function. This
392 // is often likely to be indicative of the true
393 // resolution.
394 if (gettimeofday(&a,NULL))
395 boost::throw_exception(std::runtime_error("Couldn't get resolution."));
396
397 // Spin around in a tight loop until we observe a change
398 // in the reported timer value.
399 do {
400 if (gettimeofday(&b,NULL))
401 boost::throw_exception(std::runtime_error("Couldn't get resolution."));
402 delta = double(b.tv_sec - a.tv_sec) + double(b.tv_usec - a.tv_usec)*1E-6;
403 } while (delta <= 0.0);
404
405 return delta;
406 }
407
408 private:
409 timeval start_time;
410 };
411
412}
413
414#else // BOOST_HAS_GETTIMEOFDAY
415
416// For platforms other than Windows or Linux, simply fall back to boost::timer
417#include <boost/timer.hpp>
418
419namespace util
420{
421 struct high_resolution_timer
422 : boost::timer
423 {
424 static double now()
425 {
426 return double(std::clock());
427 }
428 };
429}
430
431#endif
432
433#endif
434
435#endif // HIGH_RESOLUTION_TIMER_AUG_14_2009_1108AM
436
437