Home > Windows API > Performance measurement functions in windows

Performance measurement functions in windows

Sometimes it does become mandatory to check performance or log performance of certain function calls, something like mini profiling.

There are some functions in windows which helps us in doing this. I will rank them in their importance…

  1. QueryPerformanceCounter
  2. GetTickCount
  3. clock

Of these three QueryPerformanceCounter is the best providing us with a high resolution counter. Note that QueryPerformanceCounter itself takes some overhead so we’ve got to adjust time returned with this call’s overhead. Something like wrapping a dummy call to QueryPerformanceCounter in a QueryPerformanceCounter profiling block just once.


QueryPerformanceCounter(...); // Start counter
QueryPerformanceCounter(...); // Dummy call
// Record time taken and keep this value for later use so that we get exact time taken values

GetTickCount() is a quick function which returns the number of ticks elapsed since our application started.

clock() is similar to GetTickCount().

Let’s see how to use these functions.

1. QueryPerformanceCounter

A companion function to be used with QueryPerformanceCounter is QueryPerformanceFrequency which returns the frequency of the high-resolution performance counter. I will paste here my Performance class source code, since it will be more useful to you.

class Performance

            ZeroMemory( &m_liFrequency, sizeof( m_liFrequency ));
            VERIFY( QueryPerformanceFrequency( &m_liFrequency ));

        void Init()
            ZeroMemory( &m_liStartTime, sizeof( m_liStartTime ));
            ZeroMemory( &m_liEndTime, sizeof( m_liEndTime ));

        void Start()
            Init(); // Initialize

            // Start now
            VERIFY( QueryPerformanceCounter( &m_liStartTime ));

        double Stop()
            // Record time elapsed
            VERIFY( QueryPerformanceCounter( &m_liEndTime ));

            LARGE_INTEGER liDifference = { 0 };
            liDifference.QuadPart = ( m_liEndTime.QuadPart - m_liStartTime.QuadPart );
            return ( SCAST( double, liDifference.QuadPart )
                     SCAST( double, m_liFrequency.QuadPart ));


        LARGE_INTEGER m_liStartTime;
        LARGE_INTEGER m_liEndTime;
        LARGE_INTEGER m_liFrequency;
};// End class Performance 

// Usage
// Performance PerfObj;
// PerfObj.Start();
// CallPerformanceTestingOperationHere();
// PerfObj.Stop(); Returns time difference

You can download the performance class from here!

2. GetTickCount()

Should be sufficient if you don’t need a high resolution timer.

// Usage:
const DWORD dwStart = GetTickCount();

// Stop if this has taken too long
if( GetTickCount() - dwStart >= TIMELIMIT )

3. clock()

Much like GetTickCount except that it returns clock_t a typedef.

  // Measure the duration of an event.
  // #include <time .h>
  long i = 6000000L;

  printf( "Time to do %ld empty loops is ", i );

  // Record starting time
  const clock_t start = clock();

  // Test loop
  while( i-- )

  // Record time now
  const clock_t finish = clock();
  const double duration = (double)(finish - start) / CLOCKS_PER_SEC;
  printf( "%2.1f seconds\n", duration );

Hope these functions helps you to measure performance in an effective way, but do go through MSDN documentation for detailed information on these functions. There are some caveats to keep in mind while using these functions most of them are related to timer resolutions and hardware dependency.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: