fork download
  1. // just messing with some code at blog
  2. // http://k...content-available-to-author-only...s.com/2012/02/06/exploring-c11-part-1-time/#more-458
  3. #include <iostream>
  4. #include <chrono>
  5. #include <thread>
  6.  
  7. // used for not yet finished (and for chrono) irrelevant time printouts
  8. #include <mutex>
  9. #include <iomanip>
  10. #include <ctime>
  11.  
  12.  
  13. namespace g2
  14. {
  15. typedef std::chrono::high_resolution_clock clock;
  16. typedef std::chrono::microseconds microseconds;
  17. typedef std::chrono::milliseconds milliseconds;
  18.  
  19. clock::time_point now(){return clock::now();}
  20.  
  21. microseconds intervalUs(const clock::time_point& t1, const clock::time_point& t0)
  22. {return std::chrono::duration_cast<microseconds>(t1 - t0);}
  23.  
  24. milliseconds intervalMs(const clock::time_point& t1,const clock::time_point& t0)
  25. {return std::chrono::duration_cast<milliseconds>(t1 - t0);}
  26.  
  27.  
  28. template<typename Duration>
  29. void short_sleep(Duration d) // thanks to Anthony Williams for the suggestion of short_sleep
  30. {
  31. clock::time_point const start=clock::now(), stop=start+d;
  32. do{
  33. std::this_thread::yield();
  34. } while(clock::now()<stop);
  35. }
  36.  
  37. class StopWatch
  38. {
  39. clock::time_point start_;
  40. public:
  41. StopWatch() : start_(clock::now()){}
  42. clock::time_point restart() { start_ = clock::now(); return start_;}
  43. microseconds elapsedUs() { return intervalUs(now(), start_);}
  44. milliseconds elapsedMs() {return intervalMs(now(), start_);}
  45. };
  46. } // g2
  47.  
  48. std::mutex io_mutex;
  49. void greeting(const char* message){
  50. std::lock_guard<std::mutex> lk(io_mutex);
  51. std::cout<<message<<std::endl;
  52. }
  53.  
  54.  
  55.  
  56. void silly_sleep_ms_original_and_ugly(unsigned int ms)
  57. {
  58. using std::chrono::high_resolution_clock;
  59. using std::chrono::milliseconds;
  60.  
  61. auto t0 = high_resolution_clock::now();
  62. std::this_thread::sleep_for(milliseconds(ms));
  63. auto t1 = high_resolution_clock::now();
  64. milliseconds total_ms = std::chrono::duration_cast<milliseconds>(t1 - t0);
  65.  
  66. std::cout <<"this_thread_sleep (" << ms <<") milliseconds: "
  67. << total_ms.count() << "ms\n";
  68. }
  69.  
  70. void silly_sleep_ms(unsigned int ms)
  71. {
  72. using namespace g2;
  73. StopWatch measure;
  74. std::this_thread::sleep_for(milliseconds(ms));
  75. std::cout <<" 2this_thread_sleep (" << ms <<") milliseconds: "
  76. << measure.elapsedMs().count() << "ms\n";
  77. }
  78.  
  79.  
  80. void silly_sleep_us(unsigned int us)
  81. {
  82. using namespace g2;
  83. StopWatch measure;
  84. std::this_thread::sleep_for(microseconds(us));
  85. std::cout <<" this_thread_sleep (" << us <<") microseconds: "
  86. << measure.elapsedUs().count() << "us\n";
  87. }
  88.  
  89. void silly_improved_short_sleep_us(unsigned int us)
  90. {
  91. using namespace g2;
  92. StopWatch measure;
  93. short_sleep(microseconds(us));
  94. auto elapsed_us = measure.elapsedUs().count();
  95.  
  96. std::cout <<"SHORT this_thread_sleep (" << us <<") microseconds: "
  97. << elapsed_us << "us\n";
  98. }
  99.  
  100.  
  101. int main()
  102. {
  103. silly_sleep_ms_original_and_ugly(50);
  104. silly_sleep_ms_original_and_ugly(50);
  105. silly_sleep_ms_original_and_ugly(50);
  106. silly_sleep_ms(50);
  107. silly_sleep_ms(50);
  108. silly_sleep_ms(50);
  109. silly_sleep_us(50);
  110. silly_sleep_us(50);
  111. silly_sleep_us(50);
  112. silly_improved_short_sleep_us(50);
  113. silly_improved_short_sleep_us(50);
  114. silly_improved_short_sleep_us(50);
  115. {
  116. g2::StopWatch watch;
  117. std::this_thread::sleep_for(g2::microseconds(50));
  118. std::cout << "sleep(50us) took: " << watch.elapsedUs().count() << " us" << std::endl;
  119.  
  120. g2::clock::time_point t0 = g2::now();
  121. g2::short_sleep(g2::microseconds(50));
  122. g2::clock::time_point t1 = g2::now();
  123. std::cout << "intervalUs check - short_sleep(50us) took: " << g2::intervalUs(t1, t0).count() << " us" << std::endl;
  124. }
  125.  
  126. g2::StopWatch watch;
  127. g2::clock::time_point t0 =g2:: clock::now();
  128. std::this_thread::sleep_for(g2::microseconds(50));
  129. std::cout << g2::intervalUs(g2::now(), t0).count() << "us\n";
  130. std::cout << watch.elapsedUs().count();
  131. std::time_t t = std::time(NULL);
  132. std::cout << "UTC: " << std::put_time(std::gmtime(&t), "%c %Z" )<< '\n';
  133. std::cout << "local: " << std::put_time(std::localtime(&t), "%c %Z") << '\n';
  134.  
  135.  
  136. return 0;
  137. }
  138.  
  139.  
Success #stdin #stdout 0.01s 5476KB
stdin
Standard input is empty
stdout
this_thread_sleep (50) milliseconds: 50ms
this_thread_sleep (50) milliseconds: 50ms
this_thread_sleep (50) milliseconds: 50ms
 2this_thread_sleep (50) milliseconds: 50ms
 2this_thread_sleep (50) milliseconds: 50ms
 2this_thread_sleep (50) milliseconds: 50ms
 this_thread_sleep (50) microseconds: 107us
 this_thread_sleep (50) microseconds: 104us
 this_thread_sleep (50) microseconds: 104us
SHORT this_thread_sleep (50) microseconds: 50us
SHORT this_thread_sleep (50) microseconds: 50us
SHORT this_thread_sleep (50) microseconds: 50us
sleep(50us) took: 67 us
intervalUs check - short_sleep(50us) took: 50 us
104us
110UTC:   Sun Dec  4 15:19:56 2022 GMT
local: Sun Dec  4 15:19:56 2022 UTC