Stopwatch.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #pragma once
  2. #include "Time.h"
  3. namespace baselib
  4. {
  5. BASELIB_CPP_INTERFACE
  6. {
  7. // Stopwatch
  8. // Simplistic stopwatch tool to take accurate time measurements using Baselib_Timer
  9. //
  10. // Usage example:
  11. // auto watch = Stopwatch::StartNew();
  12. // HeavyOperation();
  13. // printf("Time passed: %fs", watch.GetElapsedTime().ToSeconds());
  14. class Stopwatch
  15. {
  16. public:
  17. static Stopwatch StartNew() { return Stopwatch(); }
  18. high_precision_clock::duration GetElapsedTime() const
  19. {
  20. return high_precision_clock::duration_from_ticks(high_precision_clock::now_in_ticks() - m_StartTime);
  21. }
  22. high_precision_clock::duration Restart()
  23. {
  24. high_precision_clock::duration elapsed = GetElapsedTime();
  25. m_StartTime = high_precision_clock::now_in_ticks();
  26. return elapsed;
  27. }
  28. private:
  29. Stopwatch() : m_StartTime(high_precision_clock::now_in_ticks()) {}
  30. Baselib_Timer_Ticks m_StartTime;
  31. };
  32. }
  33. }