Tuesday, April 19, 2011

time measure QueryPerformanceCounter example c c++ win32

QueryPerformanceCounter() and QueryPerformanceFrequency() function provides very accurate time measure in Windows platform.QueryPerformanceFrequency() returns number of counts per second which depends on the system. QueryPerformanceCounter() returns current counter value. The accuracy is in 100us units. Time measure sample code is shown below.


// Time measure routine on Windows Platform.
//
DWORD dwEllapsedTime;
LARGE_INTEGER ilFreq;
LARGE_INTEGER ilStartTime, ilEndTime;

QueryPerformanceFrequency(&ilFreq); // counts per seconds.
QueryPerformanceCounter(&ilStartTime);

// Write your code for which to measure execution time.

QueryPerformanceCounter(&ilEndTime);

// Compute ellapsed time.
//
dwEllapsedTime = (double)((ilEndTime.QuadPart-ilStartTime.QuadPart)/(ilFreq.QuadPart/10000));