time Get Time versus Get Tick Count

 I have a confession to make – I only just found out the difference between the Windows function GetTickCount and timeGetTime.

I know what you’re thinking – I’m a game developer and I should know better than that. But I didn’t. And I’m sorry.

Naturally I’m handling my ignorance by writing some test code and then blogging about it. My therapist thought it would help me resolve the inner conflicts which this has triggered.


I got started on this investigation because of a post on Larry Osterman’s blog. His test code spins in a loop calling Sleep(53) and then measuring how much GetTickCount and timeGetTime change during each iteration. His observation was that dTime was extremely consistent, but dTick varied wildly. My results were different, showing dTick and dTime being slightly different, but equally consistent:


Without giving away too much I can tell you that the problem with Larry’s code is that it tests not just GetTickCount and timeGetTime, but also the behavior of Sleep(53), and hence the entire Windows scheduler, which is known to be complicated. And, Larry’s computer was (apparently without him realizing) running in a non-recommended configuration. On the other hand, his basic conclusions are correct.


Better testation

Both GetTickCount and timeGetTime return unsigned integers that represent a millisecond count. So, it can be interesting to call them in a busy loop and see what values (deltas from a baseline actually) we get back, thus mostly taking the scheduler out of the equation. This is easily done with this code fragment:


I think array that’s totally cool. ‘1’ shows up for millisecond offsets where GetTickCount returned a value, ‘2’ shows up for millisecond offsets where timeGetTime returned a value, and ‘3’ shows up for millisecond offsets where they both returned a value. Given that the table is 32 elements wide we can see that the results of both functions change about every 15-16 ms, giving us two nearly vertical lines.

This result is because these functions return a timer value that is updated by an interrupt, and on my laptop, according to clockres, that interrupt fires every 15.6 ms. If we look closely we can see that the columns tilt left about 0.8 columns every row, which is about 0.4 ms every 16 ms.

In other words, despite calling GetTickCount and timeGetTime literally millions of times per second, the values returned only changed about every 15.6 ms.

So far GetTickCount and timeGetTime look pretty much identical, with a sometimes one millisecond offset.


Comments

Popular posts from this blog

org codeaurora btmultisim

Does coding require math