Advertisement
bartekltg

stoper

Jul 5th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1.  
  2. #include <chrono>
  3.  
  4.  
  5. using namespace std;
  6. class timer
  7.  
  8. {
  9.     chrono::time_point<chrono::high_resolution_clock> a,b;
  10. public:
  11.     void start(){a = chrono::high_resolution_clock::now();}
  12.     void stop() {b = chrono::high_resolution_clock::now();}
  13.     double value()
  14.     {
  15.         chrono::duration<double> elapsed_seconds = b-a;
  16.         return elapsed_seconds.count();
  17.     }
  18.     void show()
  19.     {
  20.         cout << value()<<" s"<<endl;
  21.     }
  22. };
  23.  
  24.  
  25.  
  26. class timer_cumulative
  27. {
  28.     chrono::time_point<chrono::high_resolution_clock> a,b;
  29.     double cumul_time;
  30.     double elapsed_sec()
  31.     {
  32.         chrono::duration<double> elapsed_seconds = b-a;
  33.         return elapsed_seconds.count();
  34.     }
  35. public:
  36.     void reset(){cumul_time = 0;}
  37.     timer_cumulative(){reset();}
  38. //    void start(){a = chrono::high_resolution_clock::now(); reset();}
  39. //    void resume() {a = chrono::high_resolution_clock::now();}
  40.     void start(){a = chrono::high_resolution_clock::now();}
  41.     void stop() {b = chrono::high_resolution_clock::now(); cumul_time += elapsed_sec();}
  42.     void show()
  43.     {
  44.         cout << cumul_time<<" s"<<endl;
  45.     }
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement