9

I have a question regarding GetTickCount function, I have two calls to this function in my code with several commands between them and the function in both calls returns same count. i.e.

var1 = GetTickCount();
code
:
:
var2 = GetTickCount();

var1 and var2 has same values in it.

can someone help?

greatwolf
  • 20,287
  • 13
  • 71
  • 105
Leonid
  • 93
  • 1
  • 1
  • 3

5 Answers5

18

Assuming this is the Windows GetTickCount call, that's entirely reasonable:

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.

Note that it's only measuring milliseconds to start with - and you can do an awful lot in a millisecond these days.

The docs go on to say:

If you need a higher resolution timer, use a multimedia timer or a high-resolution timer.

Perhaps QueryPerformanceCounter would be more appropriate?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
7

If you are referring to the Windows API call then read this. I would guess that you are trying to time a short interval so this paragraph is relevant. Are you timing something shorter than that interval? If so look into QueryPerformanceCounter instead perhaps.

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. The resolution of the GetTickCount function is not affected by adjustments made by the GetSystemTimeAdjustment function.

jcoder
  • 29,554
  • 19
  • 87
  • 130
5

If you go the QueryPerformanceCounter route you need to watch out for hardware dependent wierdness. Its been awhile so I don't know if this kinda stuff still happens.

You might also want to take a look at this link since it has a nice sample app which compares QueryPerformanceCounter, GetTickCount and TimeGetTime

Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
  • Unfortunately the link to Nvidia isn't valid anymore. Has somebody found an alternative source? – blerontin Jul 28 '15 at 07:38
  • 1
    @blerontin I updated the links. The second one is using the web archive . It was 5 years ago so it might not work on current hardware. – Conrad Frix Jul 28 '15 at 12:47
2

From 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. The resolution of the GetTickCount function is not affected by adjustments made by the GetSystemTimeAdjustment function.

The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days. To avoid this problem, use the GetTickCount64 function. Otherwise, check for an overflow condition when comparing times.

If you need a higher resolution timer, use a multimedia timer or a high-resolution timer.

naivnomore
  • 1,291
  • 8
  • 14
1

GetTickCount has a resolution of one millisecond (in practice, it's several milliseconds). It's highly likely that the functions you're calling in between are taking considerably less than 1 millisecond.

Dmitry Brant
  • 7,612
  • 2
  • 29
  • 47