Advertisement
czaffik

cpp timer

Mar 24th, 2019
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class Timer
  7. {
  8. public:
  9.     Timer() : seconds(0) {}
  10.     ~Timer() {}
  11.  
  12.     void start() { s = std::chrono::system_clock::now(); }
  13.     void stop()
  14.     {
  15.         e = std::chrono::system_clock::now();
  16.         std::chrono::duration<double> diff = (e - s);
  17.         seconds = diff.count();
  18.     }
  19.  
  20.     double elapsedSeconds() { return seconds; }
  21.  
  22. private:
  23.     std::chrono::time_point<std::chrono::system_clock> s, e;
  24.     double seconds;
  25. };
  26.  
  27. int main(void)
  28. {
  29.     Timer timer;
  30.     timer.start();
  31.     timer.stop();
  32.     cout << timer.elapsedSeconds() << endl;
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement