Timer Question

I ran into something a bit funny.

I have my code set to delay for the difference of 500 and the

elapsed time in ms at the end of every drawing loop – and since there
isn’t any input or anything else distracting, I figured it’d delay for
490 to 510 ms per loop. However, upon running the program, I get values
ranging from as low as 440 ms up to 500! (Happily, though, I haven’t
seen it go over 500 :wink:

Anyhow, I was wondering exactly why there was such a disparity?  I

[Code Snippet]
. . .
SDL_SetTimer (10, timerfunc);
for (z = 0; z < 10; z++)
{
. . .
/* Blits, Map generation, etc – ~150 ms of stuff /*
. . .
t = etime * 10;
SDL_Delay ((500 - t < 0)?(0):(500 - t));
u = etime;
printf (“Time in loop %i : %i\n”, z, t);
printf (“Time after loop %i : %i\n”, z, u*10);
etime = 0;
}
. . .
[end code]

**** OUTPUT ****

Time after loop 5 : 490
Time in loop 6 : 150
Time after loop 6 : 450
Time in loop 7 : 160
Time after loop 7 : 500
Time in loop 8 : 140
Time after loop 8 : 440
Time in loop 9 : 150
Time after loop 9 : 500

“Kendall E. Blake” wrote:

I ran into something a bit funny.

I have my code set to delay for the difference of 500 and the

elapsed time in ms at the end of every drawing loop – and since there
isn’t any input or anything else distracting, I figured it’d delay for
490 to 510 ms per loop. However, upon running the program, I get values
ranging from as low as 440 ms up to 500! (Happily, though, I haven’t
seen it go over 500 :wink:

Don’t act so surprised. Linux, Windoze, and all of the platforms SDL
runs, none of these things is an RTOS. Don’t expect to have anything
even approximating microsecond, or in many cases, even millisecond
precision in your timing loops. The timer functions are not exactly
what you would call accurate. What does surprise me is that you’ve
never seen a timer overrun. I’ve been getting those so much in my 3D
animation library that I just gave up and stopped even trying to
generate a real-time frame rate. Let the machine do what it will, and
in the end all I can say is
sorry-if-it-looks-too-jerky-just-buy-a-faster-computer.

Hey, maybe your computer is so fast that it can do an infinite loop in
five seconds! ;-)–

| Rafael R. Sevilla @Rafael_R_Sevilla |
| Instrumentation, Robotics, and Control Laboratory |

College of Engineering, University of the Philippines, Diliman