How to get high resolution timing info

We know that we can use GetTickCount to get timing info but the resolution of this API is limited to about 10 milliseconds, see MSDN:

The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds.

To get higher resolution timing info, two popular options are:

1. The multimedia timeGetTime function, you can use this API to get 1 millisecond resolution timing info:

        TIMECAPS timeCaps = {0};


        //use timeGetDevCaps to get the minimum and maximum

        //resolutions supported by the system

        timeGetDevCaps(&timeCaps, sizeof(timeCaps));


        printf("minimum device resolution is %d millisecond(s)\r\n",timeCaps.wPeriodMin);

        printf("maximum device resolution is %d millisecond(s)\r\n",timeCaps.wPeriodMax);


        //use timeBeginPeriod to adjust the timer resolution

        //you cannot set the resolution smaller than timeCaps.wPeriodMin

        MMRESULT result = timeBeginPeriod(timeCaps.wPeriodMin);


        if(result == TIMERR_NOERROR)


               printf("time resolution set to: %d millisecond(s)\r\n",timeCaps.wPeriodMin);



        DWORD ticks1 = timeGetTime();


        //doing some operation here



        DWORD ticks2 = timeGetTime();


        printf("time elapsed: %d\r\n",ticks2 - ticks1);


        //use timeEndPeriod to clear the resolution adjusted by the previous call //timeBeginPeriod



Internally, when you call timebeginPeriod to adjust the timer resolution, it calls into ntdll!NtSetTimerResolution which goes into kernel to manipulate the timer hardware. This resolution adjustment can affect the OS thread scheduling behavior and it can affect other applications, applications such as Windows Media Player uses these APIs and we don't see bad effect.


2. To get even higher resolution timing info, you can use QueryPerformanceFrequency and QueryPerformanceCounter:


         LARGE_INTEGER frequencyPerSec = {0};

        LARGE_INTEGER start = {0};

        LARGE_INTEGER end = {0};






               //do something here




              printf("elaplsed time: %I64d second(s)\r\n",

                        (end.QuadPart - start.QuadPart)/frequencyPerSec.QuadPart);



These APIs can get higher resolution but their overhead is higher than timeGetTime also.


Another option is the rdtsc counter but the currently hardware may have problems for this counter. Currently, SQL Server 2008 uses timtGetTime like mechanism and QueryPerformanceFrequency/QueryPerformanceCounter to get timing info, it does not use rdtsc anymore:


Issues caused by rdtsc:



Comments (0)

Skip to main content