clock.hpp 1.2 KB

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