realbasic-nug
[Top] [All Lists]

Re: Timers (probably revisited)

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Timers (probably revisited)
From: Richard <rfs at yadtel dot net>
Date: Wed, 31 Aug 2005 18:07:19 -0400
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <EC5FDC70-AA3F-4043-917E-4C059AADCA5A at yadtel dot net> <a06200733bf3bd50d48fd at [10 dot 0 dot 1 dot 4]>

On Aug 31, 2005, at 5:47 PM, Joseph J. Strout wrote:

At 5:32 PM -0400 8/31/05, Richard wrote:


My timer code copies a small picture to a canvas, adds some graphics to it, the draws it into my main window. The timer is set to repeat at 16mSec. I snatch the Microseconds count at the start and the end of the code, and use that to display the elapsed time inside the code and outside. The execution time of the code averages around 4mSec and the delay between executions is around 27mSec. Why isn't the total cycle time close to my 16mSec period that I set?


16 msec is the system "heartbeat" -- the maximum speed at which the main event loop can fire. If you want your timer to fire every time through the main event loop, you shouldn't set it right to this frequency, as that's just asking for trouble (such as it skipping one beat because you came in just over the wire, so to speak -- or "missed it by *that* much", as Max Smart would say). Set it to 1 msec instead, and then it will fire as fast as it can, which will often be 16 msec apart, though sometimes not, depending on what else is going on.

Does this mean that any and all events can only occur every 16msec? Say, if I have serial data available just after a system tick, then I won't get a DataAvail event until the next tick?



2nd question. Running this on Windows I get 0mSec execution time and 31mSec between executions. Changing my timer period to 15mSec and I get 0mSec execution time and 16mSec between. Looks like the timer resolution on Windows is 16mSec.


I thought it was actually 10 mSec on Windows, but I could believe 16. Either way, set your timer to something lower and you'll get more reliably results.


(This has the effect on Windows of my animation running fast, or slow, with no in-between.)


Your animation shouldn't depend on the exact frame rate. Instead, just measure how long it's been since the last frame, and move your objects accordingly. Then the speed will look constant despite small variations on the exact firing time.

Best,
- Joe

--
Joe Strout                          REAL Software, Inc.


Thanks for that info Joe. But how do I measure the time since the last frame if the Microseconds count (on Windows) has a resolution of 16000 microseconds? In my app it looks like the proper update period is going to be (well, should be) about half way between 16mSec and 32Msec, cause a 16mSec period is too fast and 32mSec is too slow. I suppose I can live with either one or the other, but do I have to?

-Richard
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>