1 /*=============================================================================
2 Copyright (c) 2005-2007 Hartmut Kaiser
3 2007, 2009 Tim Blechmann
5 Distributed under the Boost Software License, Version 1.0.
6 (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #if !defined(BOOST_HIGH_RESOLUTION_TIMER_HPP)
10 #define BOOST_HIGH_RESOLUTION_TIMER_HPP
12 #include <boost/config.hpp>
13 #include <boost/throw_exception.hpp>
15 #if _POSIX_C_SOURCE >= 199309L
24 class high_resolution_timer
27 high_resolution_timer()
34 int status
= clock_gettime(CLOCK_REALTIME
, &start_time
);
37 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
40 double elapsed() const // return elapsed time in seconds
44 int status
= clock_gettime(CLOCK_REALTIME
, &now
);
47 boost::throw_exception(std::runtime_error("Couldn't get current time"));
51 double ret_sec
= double(now
.tv_sec
- start_time
.tv_sec
);
52 double ret_nsec
= double(now
.tv_nsec
- start_time
.tv_nsec
);
60 double ret
= ret_sec
+ ret_nsec
/ 1e9
;
65 double elapsed_max() const // return estimated maximum value for elapsed()
67 return double((std::numeric_limits
<double>::max
)());
70 double elapsed_min() const // return minimum value for elapsed()
76 struct timespec start_time
;
81 #elif defined(__APPLE__)
83 #import <mach/mach_time.h>
88 class high_resolution_timer
91 high_resolution_timer(void)
93 mach_timebase_info_data_t info
;
95 kern_return_t err
= mach_timebase_info(&info
);
97 throw std::runtime_error("cannot create mach timebase info");
99 conversion_factor
= (double)info
.numer
/(double)info
.denom
;
105 start
= mach_absolute_time();
108 double elapsed() const // return elapsed time in seconds
110 uint64_t now
= mach_absolute_time();
111 double duration
= double(now
- start
) * conversion_factor
;
116 double elapsed_max() const // return estimated maximum value for elapsed()
118 return double((std::numeric_limits
<double>::max
)());
121 double elapsed_min() const // return minimum value for elapsed()
128 double conversion_factor
;
133 #elif defined(BOOST_WINDOWS)
141 ///////////////////////////////////////////////////////////////////////////////
143 // high_resolution_timer
144 // A timer object measures elapsed time.
145 // CAUTION: Windows only!
147 ///////////////////////////////////////////////////////////////////////////////
148 class high_resolution_timer
151 high_resolution_timer()
153 start_time
.QuadPart
= 0;
154 frequency
.QuadPart
= 0;
156 if (!QueryPerformanceFrequency(&frequency
))
157 boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
164 if (!QueryPerformanceCounter(&start_time
))
165 boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
168 double elapsed() const // return elapsed time in seconds
171 if (!QueryPerformanceCounter(&now
))
172 boost::throw_exception(std::runtime_error("Couldn't get current time"));
174 return double(now
.QuadPart
- start_time
.QuadPart
) / frequency
.QuadPart
;
177 double elapsed_max() const // return estimated maximum value for elapsed()
179 return (double((std::numeric_limits
<LONGLONG
>::max
)())
180 - double(start_time
.QuadPart
)) / double(frequency
.QuadPart
);
183 double elapsed_min() const // return minimum value for elapsed()
185 return 1.0 / frequency
.QuadPart
;
189 LARGE_INTEGER start_time
;
190 LARGE_INTEGER frequency
;
197 // For other platforms, simply fall back to boost::timer
198 #include <boost/timer.hpp>
199 #include <boost/throw_exception.hpp>
202 typedef boost::timer high_resolution_timer
;
207 #endif // !defined(BOOST_HIGH_RESOLUTION_TIMER_HPP)