clock.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright Oliver Kowalke 2009.
  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 <chrono>
  10. #include <cstddef>
  11. #include <cstdint>
  12. #include <numeric>
  13. #include <vector>
  14. #include <boost/assert.hpp>
  15. typedef std::chrono::steady_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. std::uint64_t operator()()
  21. {
  22. time_point_type start( clock_type::now() );
  23. return ( clock_type::now() - start).count();
  24. }
  25. };
  26. duration_type overhead_clock()
  27. {
  28. std::size_t iterations( 10);
  29. std::vector< std::uint64_t > overhead( iterations, 0);
  30. for ( std::size_t i = 0; i < iterations; ++i)
  31. std::generate(
  32. overhead.begin(), overhead.end(),
  33. clock_overhead() );
  34. BOOST_ASSERT( overhead.begin() != overhead.end() );
  35. return duration_type( std::accumulate( overhead.begin(), overhead.end(), 0) / iterations);
  36. }
  37. #endif // CLOCK_H