2010/03/24

QueryPerformanceCounter 이용한 StopWatch 클래스

#pragma once

class CAStopWatch
{
private:
int m_Initialized;
BOOL m_BeginFlag;
__int64 m_Frequency;
__int64 m_BeginTime;
__int64 m_EndTime;

__int64 m_i64Elapsed;
double m_dblElapsed;

public:
CAStopWatch(void)
{
// Get CPU Frequency
m_Initialized = QueryPerformanceFrequency((LARGE_INTEGER *)&m_Frequency);

m_BeginFlag = FALSE;
}

virtual ~CAStopWatch(void)
{
}

BOOL Begin()
{
if( !m_Initialized )
{
return 0;
}

// Get Begin Counter
m_BeginFlag = QueryPerformanceCounter( (LARGE_INTEGER *)&m_BeginTime );

return m_BeginFlag;
}

double End()
{
if( !m_Initialized || !m_BeginFlag )
{
return 0.0;
}

// Get End Counter
QueryPerformanceCounter( (LARGE_INTEGER *)&m_EndTime );

// calculate elapse count
m_i64Elapsed = m_EndTime - m_BeginTime;

// Convert count to second
m_dblElapsed = (double)m_i64Elapsed / (double)m_Frequency;

return m_dblElapsed;
}

BOOL Available()
{
return m_Initialized;
}

__int64 GetFreq()
{
return m_Frequency;
}

double GetElapsedTime()
{
return m_dblElapsed;
}

double GetElapsedSecond()
{
return m_dblElapsed;
}

double GetElapsedMillisecond()
{
return m_dblElapsed * 1000;
}

double GetElapsedMicroSecond()
{
return m_dblElapsed * 1000000;
}
};

// 사용 예
CAStopWatch watch;

watch.Begin();
:
:
watch.End();

double sec = watch.GetElapsedSecond();
<

Original Post : http://neodreamer-dev.tistory.com/391

No comments :

Post a Comment