2010/02/22

QueryPerformanceCounter 함수를 이용한 시간 측정


#include
#include

__int64 ctr1 = 0, ctr2 = 0, freq = 0;
int acc = 0, i = 0;

// Start timing the code.
if (QueryPerformanceCounter((LARGE_INTEGER *)&ctr1)!= 0)
{
// Code segment is being timed.
for (i=0; i<100; i++) acc++;

// Finish timing the code.
QueryPerformanceCounter((LARGE_INTEGER *)&ctr2);

Console::WriteLine("Start Value: {0}",ctr1.ToString());
Console::WriteLine("End Value: {0}",ctr2.ToString());

QueryPerformanceFrequency((LARGE_INTEGER *)&freq);

Console::WriteLine("QueryPerformanceCounter minimum resolution: 1/{0} Seconds.",freq.ToString());
Console::WriteLine("100 Increment time: {0} seconds.",((ctr2 - ctr1) * 1.0 / freq).ToString());
}
else
{
DWORD dwError = GetLastError();
Console::WriteLine("Error value = {0}",dwError.ToString());
}

// Make the console window wait.
Console::WriteLine();
Console::Write("Press ENTER to finish.");
Console::Read();

return 0;

////////////////////////////////////////////////////////////////
// Result
Start Value: 192244814693193
End Value: 192244814694677
QueryPerformanceCounter minimum resolution: 1/1263460000 Seconds.
100 Increment time: 1.17455241954633E-06 seconds.



 

QueryPerformanceCounter 를 활용한 시간 측정 코드 이다. CPU 의 동작속도를 이용하여 시간을 측정하는 방법으로 GetTickCount 보다 정확한 시간을 측정할 수 있다.



No comments :

Post a Comment