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