Aleks11

Aleksey | High accuracy timer

Oct 14th, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. class HRTimer {
  2. public:
  3.     HRTimer() : frequency(GetFrequency()) { }
  4.     LONGLONG GetFrequency()
  5.     {
  6.         LARGE_INTEGER proc_freq;
  7.         QueryPerformanceFrequency(&proc_freq);
  8.         return proc_freq.QuadPart;
  9.     }
  10.     void Reset()
  11.     {
  12.         DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0);
  13.         QueryPerformanceCounter(&start);
  14.         SetThreadAffinityMask(GetCurrentThread(), oldmask);
  15.     }
  16.     double GetElapsed()
  17.     {
  18.         DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0);
  19.         QueryPerformanceCounter(&stop);
  20.         SetThreadAffinityMask(GetCurrentThread(), oldmask);
  21.         return ((stop.QuadPart - start.QuadPart) / (double)frequency);
  22.     }
  23. private:
  24.     LARGE_INTEGER start;
  25.     LARGE_INTEGER stop;
  26.     LONGLONG frequency;
  27. };
Advertisement
Add Comment
Please, Sign In to add comment