Hello,
while working on some application, I encountered some weird behaviours and started tracking the problem, until I found out what it was. GetTickCount() didn't seem to properly behave. While trying to find out what it was, I used Sleep, and to my surprise, even weirder beharviours appeared!
I coded a little test program that still shocks me. Hope someone can help.
The program is:
I've tried that program in two computers. The behaviour on these computers is different (in one of them it actually works as expected!).Code:#include <windows.h> #include <stdio.h> int main() { long pret = GetTickCount(), post; while (1) { post = GetTickCount(); printf("\n%ld", post - pret /*- 100*/); pret = post; //Sleep(100); } }
Computer one (it works as I expected it to):
-With comments: The output is generally 0, sometimes 1.
-Without comments: The output is generally 0, sometimes 1.
Computer two:
-With comments: The output is generally 0, sometimes 15 or 16! (Never something else)
-Without comments: The output is an integer in the [-10, 10] interval.
The initial thing that bugged me was that 15/16. It is weird, as GetTickCount() should be returning the same value for a few iterations (since the iteration takes less than a millisecond) and then return that old value plus one. (Hence the correct output on computer one).
Then, one would suppose that removing those comments would only help me seeing the output in a more relaxed way (those printf's did fill the console quickly), but shouldn't affect the output. Well, negative numbers appeared. Is Sleep() not exact on some computers, but is in others?
I hope someone can help me, particularly with the GetTickCount() returning 0,0,...,0,15,0,...,0 problem. I really don't care much about Sleep() being exact or not, but it would still be interesting to know too.
Thanks in advance!