Advertisement
ascend4nt

C++11's Chrono - Learning about C++11's Time(ing) Features

Jan 4th, 2013
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>   // simple 'sleep_for' example
  4.  
  5. #include <cstdio>   // printf
  6.  
  7. // C++11 Chrono - Learning about C++11's Time(ing) features (see link below)
  8. // Author: Ascend4nt
  9. // https://ascend4nt.wordpress.com/2013/01/04/c11s-chrono-learning-about-c11s-timeing-features/
  10.  
  11. // Test cout() speeds.  Verbose version of exposing clock/time_point/duration use
  12. double test_cout()
  13. {
  14.     // Grab the start point time
  15.     std::chrono::high_resolution_clock::time_point clkStart = std::chrono::high_resolution_clock::now();
  16.    
  17.     // Loop prep: set boolalpha before to cut out two calls:
  18.     std::cout << std::boolalpha;
  19.    
  20.     // Loop to measure
  21.     for (int i = 1; i < 500; i++)
  22.         std::cout << "cout_test,   bool:" << bool(i&1) << "; double:" << 1.34 << std::endl;
  23.    
  24.     // Loop Cleanup
  25.     std::cout << std::noboolalpha;
  26.    
  27.     // Grab the end point time
  28.     std::chrono::high_resolution_clock::time_point clkEnd = std::chrono::high_resolution_clock::now();
  29.    
  30.     // get the difference of the 2 times (now - clkStart), convert it into the appropriate duration type
  31.     std::chrono::duration<double, std::milli> clkElapsed(
  32.         std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(clkEnd - clkStart)
  33.     );
  34.    
  35.     // and finally get the actual value
  36.     double fElapsed = clkElapsed.count();
  37.    
  38.     return fElapsed;
  39. }
  40.  
  41. // Test printf() speeds.  Less verbose version of exposing clock/time_point/duration use
  42. double test_printf()
  43. {
  44.     // Aliases make things soo much cleaner!
  45.     typedef std::chrono::high_resolution_clock hrClock_t;
  46.     // duration type: calculation will be floating point, ratio will be 1/1000th (of a second)
  47.     typedef std::chrono::duration<double, std::milli> millisecs_t;
  48.    
  49.     // Grab the start point time
  50.     auto clkStart = hrClock_t::now();
  51.    
  52.     // Loop to measure
  53.     for (int i = 1; i < 500; i++)
  54.         printf("printf_test, bool:%s; double:%.02f\n", ((i&1) ? "true" : "false"), 1.34);
  55.    
  56.     // Grab the end point time
  57.     auto clkEnd = hrClock_t::now();
  58.    
  59.     // get the difference of the 2 times (now - clkStart), convert it into the appropriate duration type
  60.     millisecs_t clkElapsed(std::chrono::duration_cast<millisecs_t>(clkEnd - clkStart));
  61.    
  62.     // and finally get the actual value
  63.     double fElapsed = clkElapsed.count();
  64.    
  65.     return fElapsed;
  66. }
  67.  
  68. int main ()
  69. {
  70.   double fcoutTime, fprintfTime;
  71.  
  72.   fcoutTime = test_cout();
  73.  
  74.   fprintfTime = test_printf();
  75.  
  76.   // Random injection of a sleep (to show another use of chrono)
  77.   std::this_thread::sleep_for(std::chrono::milliseconds(10));
  78.  
  79.   printf("test_cout elapsed time = %f, vs. test_printf elapsed time = %f\n", fcoutTime, fprintfTime);
  80.  
  81.   return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement