/extlibs/UnitTest++/source/Win32/TimeHelpers.cpp

https://bitbucket.org/hugoruscitti/pilascpp · C++ · 50 lines · 39 code · 11 blank · 0 comment · 2 complexity · 6d5813ab809ca8ecd970cddf553383f7 MD5 · raw file

  1. #include "TimeHelpers.h"
  2. #include <windows.h>
  3. namespace UnitTest {
  4. Timer::Timer()
  5. : m_startTime(0)
  6. , m_threadHandle(::GetCurrentThread())
  7. {
  8. #if defined(_MSC_VER) && (_MSC_VER == 1200) // VC6 doesn't have DWORD_PTR?
  9. typedef unsigned long DWORD_PTR;
  10. #endif
  11. DWORD_PTR systemMask;
  12. ::GetProcessAffinityMask(GetCurrentProcess(), &m_processAffinityMask, &systemMask);
  13. ::SetThreadAffinityMask(m_threadHandle, 1);
  14. ::QueryPerformanceFrequency(reinterpret_cast< LARGE_INTEGER* >(&m_frequency));
  15. ::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask);
  16. }
  17. void Timer::Start()
  18. {
  19. m_startTime = GetTime();
  20. }
  21. int Timer::GetTimeInMs() const
  22. {
  23. __int64 const elapsedTime = GetTime() - m_startTime;
  24. double const seconds = double(elapsedTime) / double(m_frequency);
  25. return int(seconds * 1000.0f);
  26. }
  27. __int64 Timer::GetTime() const
  28. {
  29. LARGE_INTEGER curTime;
  30. ::SetThreadAffinityMask(m_threadHandle, 1);
  31. ::QueryPerformanceCounter(&curTime);
  32. ::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask);
  33. return curTime.QuadPart;
  34. }
  35. void TimeHelpers::SleepMs(int const ms)
  36. {
  37. ::Sleep(ms);
  38. }
  39. }