Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. namespace Utils {
  2.  
  3. template <
  4. typename TimeUnit = std::chrono::milliseconds,
  5. typename Clock = std::chrono::high_resolution_clock
  6. >
  7. class Stopwatch
  8. {
  9. public:
  10.  
  11. explicit Stopwatch()
  12. {
  13. reset();
  14. }
  15.  
  16. inline void reset()
  17. {
  18. start_ = Clock::now();
  19. stopped_ = false;
  20. }
  21.  
  22. inline void stop()
  23. {
  24. stop_ = Clock::now();
  25. stopped_ = true;
  26. }
  27.  
  28. /// If the timer is stopped, returns the elapsed time between the start and stop times.
  29. /// Otherwise, returns the elapsed time between the start time and the current time
  30. inline TimeUnit elapsed() const
  31. {
  32. auto t = stopped_ ? stop_ : Clock::now();
  33. return std::chrono::duration_cast<TimeUnit>(t - start_);
  34. }
  35.  
  36. private:
  37. std::chrono::time_point<Clock> start_, stop_;
  38. bool stopped_ = false;
  39. };
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement