You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
841 B

1 year ago
  1. #pragma once
  2. #include <chrono>
  3. namespace vlc
  4. {
  5. struct Timer
  6. {
  7. using time_point = std::chrono::high_resolution_clock::time_point;
  8. time_point start_time;
  9. double duration = 0; // Duration between last call of end and start_time
  10. double total_duration = 0;
  11. static inline time_point now()
  12. {
  13. return std::chrono::high_resolution_clock::now();
  14. }
  15. static inline double ms_duration_between(const time_point start_time, const time_point end_time)
  16. {
  17. std::chrono::duration<double, std::milli> fp_ms = end_time - start_time;
  18. return fp_ms.count();
  19. }
  20. void reset()
  21. {
  22. duration = total_duration = 0;
  23. }
  24. void start()
  25. {
  26. start_time = now();
  27. }
  28. double stop()
  29. {
  30. auto end_time = now();
  31. duration = ms_duration_between(start_time, end_time);
  32. total_duration += duration;
  33. return duration;
  34. }
  35. };
  36. }