PageRenderTime 26ms CodeModel.GetById 22ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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