high_res_clock.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // high_res_clock.hpp
  3. // ~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef HIGH_RES_CLOCK_HPP
  11. #define HIGH_RES_CLOCK_HPP
  12. #include <boost/config.hpp>
  13. #include <boost/cstdint.hpp>
  14. #if defined(BOOST_ASIO_WINDOWS)
  15. inline boost::uint64_t high_res_clock()
  16. {
  17. LARGE_INTEGER i;
  18. QueryPerformanceCounter(&i);
  19. return i.QuadPart;
  20. }
  21. #elif defined(__GNUC__) && defined(__x86_64__)
  22. inline boost::uint64_t high_res_clock()
  23. {
  24. unsigned long low, high;
  25. __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high));
  26. return (((boost::uint64_t)high) << 32) | low;
  27. }
  28. #else
  29. #include <boost/date_time/posix_time/posix_time_types.hpp>
  30. inline boost::uint64_t high_res_clock()
  31. {
  32. boost::posix_time::ptime now =
  33. boost::posix_time::microsec_clock::universal_time();
  34. boost::posix_time::ptime epoch(
  35. boost::gregorian::date(1970, 1, 1),
  36. boost::posix_time::seconds(0));
  37. return (now - epoch).total_microseconds();
  38. }
  39. #endif
  40. #endif // HIGH_RES_CLOCK_HPP