1 2 // Copyright Oliver Kowalke 2009. 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 // 7 #ifndef CLOCK_H 8 #define CLOCK_H 9 10 #include <algorithm> 11 #include <cstddef> 12 #include <numeric> 13 #include <vector> 14 15 #include <boost/assert.hpp> 16 #include <boost/chrono.hpp> 17 #include <boost/cstdint.hpp> 18 19 typedef boost::chrono::high_resolution_clock clock_type; 20 typedef clock_type::duration duration_type; 21 typedef clock_type::time_point time_point_type; 22 23 struct clock_overhead 24 { operator ()clock_overhead25 boost::uint64_t operator()() 26 { 27 time_point_type start( clock_type::now() ); 28 return ( clock_type::now() - start).count(); 29 } 30 }; 31 32 inline overhead_clock()33duration_type overhead_clock() 34 { 35 std::size_t iterations( 10); 36 std::vector< boost::uint64_t > overhead( iterations, 0); 37 for ( std::size_t i = 0; i < iterations; ++i) 38 std::generate( 39 overhead.begin(), overhead.end(), 40 clock_overhead() ); 41 BOOST_ASSERT( overhead.begin() != overhead.end() ); 42 return duration_type( std::accumulate( overhead.begin(), overhead.end(), 0) / iterations); 43 } 44 45 #endif // CLOCK_H 46