60 lines
1.1 KiB
C++
60 lines
1.1 KiB
C++
#include "Timer.h"
|
|
#include <Windows.h>
|
|
|
|
//#define TIMER_TGT
|
|
|
|
namespace LifeCore
|
|
{
|
|
Timer::Timer()
|
|
:timeElapsed(0)
|
|
{
|
|
#ifdef TIMER_TGT
|
|
if(refCount == 0)
|
|
timeBeginPeriod(1);
|
|
refCount++;
|
|
#else
|
|
QueryPerformanceFrequency((LARGE_INTEGER *)&perfFreq);
|
|
#endif
|
|
}
|
|
Timer::~Timer()
|
|
{
|
|
#ifdef TIMER_TGT
|
|
refCount--;
|
|
if(refCount == 0)
|
|
timeEndPeriod(1);
|
|
#endif
|
|
}
|
|
void Timer::Start()
|
|
{
|
|
#ifdef TIMER_TGT
|
|
lastTime = timeGetTime();
|
|
#else
|
|
DWORD_PTR old_mask = SetThreadAffinityMask(GetCurrentThread(), 0);
|
|
QueryPerformanceCounter((LARGE_INTEGER *)&lastTime);
|
|
SetThreadAffinityMask(GetCurrentThread(), old_mask);
|
|
#endif
|
|
}
|
|
void Timer::Stop()
|
|
{
|
|
#ifdef TIMER_TGT
|
|
curTime = timeGetTime();
|
|
diff = curTime - lastTime;
|
|
timeElapsed = 1.0 / (double)diff; //time in SECONDS
|
|
#else
|
|
DWORD_PTR old_mask = SetThreadAffinityMask(GetCurrentThread(), 0);
|
|
QueryPerformanceCounter((LARGE_INTEGER *)&curTime);
|
|
SetThreadAffinityMask(GetCurrentThread(), old_mask);
|
|
if(lastTime == 0)
|
|
timeElapsed = 0;
|
|
else
|
|
{
|
|
timeElapsed = (float)((double)(curTime - lastTime) / (double)perfFreq);
|
|
}
|
|
|
|
#endif
|
|
}
|
|
float Timer::getTimeElapsed()
|
|
{
|
|
return timeElapsed;
|
|
}
|
|
} |