runtime_resolution.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // runtime_resolution.cpp ----------------------------------------------------------//
  2. // Copyright 2008 Howard Hinnant
  3. // Copyright 2008 Beman Dawes
  4. // Copyright 2009 Vicente J. Botet Escriba
  5. // Copyright (c) Microsoft Corporation 2014
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See http://www.boost.org/LICENSE_1_0.txt
  8. /*
  9. This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
  10. was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
  11. Many thanks to Howard for making his code available under the Boost license.
  12. The original code was modified to conform to Boost conventions and to section
  13. 20.9 Time utilities [time] of the C++ committee's working paper N2798.
  14. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
  15. time2_demo contained this comment:
  16. Much thanks to Andrei Alexandrescu,
  17. Walter Brown,
  18. Peter Dimov,
  19. Jeff Garland,
  20. Terry Golubiewski,
  21. Daniel Krugler,
  22. Anthony Williams.
  23. */
  24. #define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness
  25. #include <boost/chrono/chrono.hpp>
  26. #include <boost/type_traits.hpp>
  27. #include <iostream>
  28. #if defined(BOOST_CHRONO_MAC_API)
  29. #include <sys/time.h> //for gettimeofday and timeval
  30. #include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
  31. #endif
  32. #if defined(BOOST_CHRONO_WINDOWS_API)
  33. #include <windows.h>
  34. namespace
  35. {
  36. #if defined UNDER_CE || BOOST_PLAT_WINDOWS_RUNTIME
  37. // Windows CE and Windows store does not define timeval
  38. struct timeval {
  39. long tv_sec; /* seconds */
  40. long tv_usec; /* and microseconds */
  41. };
  42. #endif
  43. int gettimeofday(struct timeval * tp, void *)
  44. {
  45. FILETIME ft;
  46. #if defined(UNDER_CE)
  47. // Windows CE does not define GetSystemTimeAsFileTime so we do it in two steps.
  48. SYSTEMTIME st;
  49. ::GetSystemTime( &st );
  50. ::SystemTimeToFileTime( &st, &ft );
  51. #else
  52. ::GetSystemTimeAsFileTime( &ft ); // never fails
  53. #endif
  54. long long t = (static_cast<long long>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
  55. # if !defined( BOOST_MSVC ) || BOOST_MSVC > 1300 // > VC++ 7.0
  56. t -= 116444736000000000LL;
  57. # else
  58. t -= 116444736000000000;
  59. # endif
  60. t /= 10; // microseconds
  61. tp->tv_sec = static_cast<long>( t / 1000000UL);
  62. tp->tv_usec = static_cast<long>( t % 1000000UL);
  63. return 0;
  64. }
  65. } // unnamed namespace
  66. #endif
  67. // Handle duration with resolution not known until run time
  68. using namespace boost::chrono;
  69. namespace runtime_resolution
  70. {
  71. class duration
  72. {
  73. public:
  74. typedef long long rep;
  75. private:
  76. rep rep_;
  77. static const double ticks_per_nanosecond;
  78. public:
  79. typedef boost::chrono::duration<double, boost::nano> tonanosec;
  80. duration() {} // = default;
  81. explicit duration(const rep& r) : rep_(r) {}
  82. // conversions
  83. explicit duration(const tonanosec& d)
  84. : rep_(static_cast<rep>(d.count() * ticks_per_nanosecond)) {}
  85. // explicit
  86. tonanosec convert_to_nanosec() const {return tonanosec(rep_/ticks_per_nanosecond);}
  87. // observer
  88. rep count() const {return rep_;}
  89. // arithmetic
  90. duration& operator+=(const duration& d) {rep_ += d.rep_; return *this;}
  91. duration& operator-=(const duration& d) {rep_ += d.rep_; return *this;}
  92. duration& operator*=(rep rhs) {rep_ *= rhs; return *this;}
  93. duration& operator/=(rep rhs) {rep_ /= rhs; return *this;}
  94. duration operator+() const {return *this;}
  95. duration operator-() const {return duration(-rep_);}
  96. duration& operator++() {++rep_; return *this;}
  97. duration operator++(int) {return duration(rep_++);}
  98. duration& operator--() {--rep_; return *this;}
  99. duration operator--(int) {return duration(rep_--);}
  100. friend duration operator+(duration x, duration y) {return x += y;}
  101. friend duration operator-(duration x, duration y) {return x -= y;}
  102. friend duration operator*(duration x, rep y) {return x *= y;}
  103. friend duration operator*(rep x, duration y) {return y *= x;}
  104. friend duration operator/(duration x, rep y) {return x /= y;}
  105. friend bool operator==(duration x, duration y) {return x.rep_ == y.rep_;}
  106. friend bool operator!=(duration x, duration y) {return !(x == y);}
  107. friend bool operator< (duration x, duration y) {return x.rep_ < y.rep_;}
  108. friend bool operator<=(duration x, duration y) {return !(y < x);}
  109. friend bool operator> (duration x, duration y) {return y < x;}
  110. friend bool operator>=(duration x, duration y) {return !(x < y);}
  111. };
  112. static
  113. double
  114. init_duration()
  115. {
  116. #if defined(BOOST_CHRONO_WINDOWS_API)
  117. return static_cast<double>(1) / 1000; // Windows FILETIME is 1 per microsec
  118. #elif defined(BOOST_CHRONO_MAC_API)
  119. mach_timebase_info_data_t MachInfo;
  120. mach_timebase_info(&MachInfo);
  121. return static_cast<double>(MachInfo.denom) / MachInfo.numer;
  122. #elif defined(BOOST_CHRONO_POSIX_API)
  123. return static_cast<double>(1) / 1000;
  124. #endif
  125. }
  126. const double duration::ticks_per_nanosecond = init_duration();
  127. class clock;
  128. class time_point
  129. {
  130. public:
  131. typedef runtime_resolution::clock clock;
  132. typedef long long rep;
  133. private:
  134. rep rep_;
  135. rep count() const {return rep_;}
  136. public:
  137. time_point() : rep_(0) {}
  138. explicit time_point(const duration& d)
  139. : rep_(d.count()) {}
  140. // arithmetic
  141. time_point& operator+=(const duration& d) {rep_ += d.count(); return *this;}
  142. time_point& operator-=(const duration& d) {rep_ -= d.count(); return *this;}
  143. friend time_point operator+(time_point x, duration y) {return x += y;}
  144. friend time_point operator+(duration x, time_point y) {return y += x;}
  145. friend time_point operator-(time_point x, duration y) {return x -= y;}
  146. friend duration operator-(time_point x, time_point y) {return duration(x.rep_ - y.rep_);}
  147. };
  148. class clock
  149. {
  150. public:
  151. typedef runtime_resolution::duration::rep rep;
  152. typedef runtime_resolution::duration duration;
  153. typedef runtime_resolution::time_point time_point;
  154. static time_point now()
  155. {
  156. #if defined(BOOST_CHRONO_WINDOWS_API)
  157. timeval tv;
  158. gettimeofday( &tv, 0 );
  159. return time_point(duration((static_cast<rep>(tv.tv_sec)<<32) | tv.tv_usec));
  160. #elif defined(BOOST_CHRONO_MAC_API)
  161. timeval tv;
  162. gettimeofday( &tv, 0 );
  163. return time_point(duration((static_cast<rep>(tv.tv_sec)<<32) | tv.tv_usec));
  164. #elif defined(BOOST_CHRONO_POSIX_API)
  165. timespec ts;
  166. ::clock_gettime( CLOCK_REALTIME, &ts );
  167. return time_point(duration((static_cast<rep>(ts.tv_sec)<<32) | ts.tv_nsec/1000));
  168. #endif // POSIX
  169. }
  170. };
  171. void test()
  172. {
  173. std::cout << "runtime_resolution test\n";
  174. clock::duration delay(boost::chrono::milliseconds(5));
  175. clock::time_point start = clock::now();
  176. while (clock::now() - start <= delay)
  177. ;
  178. clock::time_point stop = clock::now();
  179. clock::duration elapsed = stop - start;
  180. std::cout << "paused " <<
  181. boost::chrono::nanoseconds(
  182. boost::chrono::duration_cast<boost::chrono::nanoseconds>( elapsed.convert_to_nanosec() )).count()
  183. << " nanoseconds\n";
  184. }
  185. } // runtime_resolution
  186. int main()
  187. {
  188. runtime_resolution::test();
  189. return 0;
  190. }