Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Stopwatch.h 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. // using fsec = std::chrono::duration<double>;
  14. // printf("Time passed: %fs", std::chrono::duration_cast<fsec>(watch.GetElapsedTime()).count());
  15. class Stopwatch
  16. {
  17. public:
  18. static Stopwatch StartNew() { return Stopwatch(); }
  19. high_precision_clock::duration GetElapsedTime() const
  20. {
  21. return high_precision_clock::duration_from_ticks(high_precision_clock::now_in_ticks() - m_StartTime);
  22. }
  23. high_precision_clock::duration Restart()
  24. {
  25. high_precision_clock::duration elapsed = GetElapsedTime();
  26. m_StartTime = high_precision_clock::now_in_ticks();
  27. return elapsed;
  28. }
  29. private:
  30. Stopwatch() : m_StartTime(high_precision_clock::now_in_ticks()) {}
  31. Baselib_Timer_Ticks m_StartTime;
  32. };
  33. }
  34. }