hp_timer.h 989 B

123456789101112131415161718192021222324252627282930313233343536
  1. #pragma once
  2. #include "defaults.h"
  3. namespace NHPTimer {
  4. using STime = i64;
  5. // May delay for ~50ms to compute frequency
  6. double GetSeconds(const STime& a) noexcept;
  7. // Returns the current time
  8. void GetTime(STime* pTime) noexcept;
  9. // Returns the time passed since *pTime, and writes the current time into *pTime.
  10. double GetTimePassed(STime* pTime) noexcept;
  11. // Get TSC frequency, may delay for ~50ms to compute frequency
  12. double GetClockRate() noexcept;
  13. // same as GetClockRate, but in integer
  14. ui64 GetCyclesPerSecond() noexcept;
  15. } // namespace NHPTimer
  16. struct THPTimer {
  17. THPTimer() noexcept {
  18. Reset();
  19. }
  20. void Reset() noexcept {
  21. NHPTimer::GetTime(&Start);
  22. }
  23. double Passed() const noexcept {
  24. NHPTimer::STime tmp = Start;
  25. return NHPTimer::GetTimePassed(&tmp);
  26. }
  27. double PassedReset() noexcept {
  28. return NHPTimer::GetTimePassed(&Start);
  29. }
  30. private:
  31. NHPTimer::STime Start;
  32. };