Advertisement
rajhlinux

C/C++ Timer

Sep 6th, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | Software | 0 0
  1. // Calculate/determine the accurate variable timers at the first execution of the program,
  2. // reguardless if the timer captured any time, print it to the screen.
  3. // Then keep printing in calculating/determining the accurate variable timers until the program is halted.
  4. //
  5.  
  6. #include <iostream>
  7. #include <ctime>
  8. #include <ratio>
  9. #include <chrono>
  10.  
  11. using namespace std::chrono;
  12.  
  13. high_resolution_clock::time_point t1 = {};  
  14. high_resolution_clock::time_point t2 = {};  
  15. high_resolution_clock::time_point t3 = {};  
  16.  
  17. int main ()
  18. {
  19.   while (1)
  20.   {
  21.     t2 = high_resolution_clock::now();
  22.  
  23.     // Calculate timer "t3":  
  24.     // How to display the correct time elapsed for "t3" for the first execution run? Current implementation will throw false readings:
  25.     // How to determine the time elapsed for "t3" without "t2"?
  26.     std::cout << "\nElapsed time for t3: " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t3).count() << " Secs\n\n";
  27.  
  28.  
  29.     t1 = high_resolution_clock::now();
  30.     int i;
  31.     for (i = 0; i <= 100; i++)
  32.     {
  33.       printf("Hello World\n");
  34.     }
  35.     t2 = high_resolution_clock::now();
  36.  
  37.     std::cout << "Elapsed time for printf(Hello World): " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << " Secs\n\n";
  38.  
  39.     // Timer t3, captures time reference at this point which will be calculated later when main() function loops again.
  40.     t3 = high_resolution_clock::now();
  41.   }
  42.  
  43.   return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement