Precise time measuring on Win32

Windows provide means for a more precise time measurement than just the standard C-function clock().

The basic definitions and declarations are as follows (typedef is for readability):

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

typedef LARGE_INTEGER timeStamp;

void getCurrentTimeStamp(timeStamp& _time);
timeStamp getCurrentTimeStamp();
double getTimeMili(const timeStamp& start, const timeStamp& end);
double getTimeSecs(const timeStamp& start, const timeStamp& end);

Notes:
* LARGE_INTEGER is 8B big, so it’s faster to use reference than direct copy, but this may vary compiler to compiler – test before using!
* Value stored in LARGE_INTEGER variable is by itself useless – it’s something of the sort “number of ticks since the computer booted”; but two of them define interval with quite a useable resolution. (check MSDN for more info)

Usage is trivial – to measure intervals, simply use one of getCurrentTimeStamp() functions to get current “timestamp” of beginning and end of interval, and then use getTimeMili() go get interval’s length in miliseconds, or getTimeSecs() for length in seconds.

Implementation pretty much just uses what’s readily available (both versions of getCurrentTimeStamp() are obvious candidates for inline‘s):

void getCurrentTimeStamp(timeStamp& _time)
{
    QueryPerformanceCounter(&_time);
}

timeStamp getCurrentTimeStamp()
{
    timeStamp tmp;
    QueryPerformanceCounter(&tmp);
    return tmp;
}

double getTimeMili(const timeStamp& start, const timeStamp& end)
{
    timeStamp dwFreq;
    QueryPerformanceFrequency(&dwFreq);
    return double(end.QuadPart - start.QuadPart) /
        double(dwFreq.QuadPart) * 1000;
}

double getTimeSecs(const timeStamp& start, const timeStamp& end)
{
    timeStamp dwFreq;
    QueryPerformanceFrequency(&dwFreq);
    return double(end.QuadPart - start.QuadPart) / double(dwFreq.QuadPart);
}

Note:
* The QueryPerformanceFrequency(&dwFreq); in getTimeMili() returns the same value on each call – storing this value at the start of the program might be a good idea. (most probably – on multiprocessor computers this might be false!)

3 responses to “Precise time measuring on Win32

  1. Thanks for the great tip! 😀

    The “* 1000” part in getTimeMili() is invisible… a change of line might help?

  2. You’re welcome, I’m glad I could be of any help.

    This whole code is in fact part of my “Commons” library, that includes logging, parsing of strings, Postgres, ODBC, assertions, etc., but I didn’t yet have time to whip up some documentation and post it.

    Thanks for the tip about “* 1000”, to me it shows just fine, but of course I want everyone to see it there; added a line break… 🙂

  3. Wow, amazing blog layout! How lengthy have you been running a blog for? you make running a blog look easy. The full glance of your web site is wonderful, as well as the content!

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