Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTimer accuracy

NSTimer accuracy
Thread Tools
JBL
Junior Member
Join Date: Sep 2000
Status: Offline
Reply With Quote
Jan 18, 2001, 08:45 PM
 
The documentation on NSTimer says "Because of the various input sources a typical run loop manages, the effective resolution of the time interval for an NSTimer is limited to on the order of 50-100 milliseconds." Is there some way of doing more accurate timing?
     
Fresh-Faced Recruit
Join Date: Jan 2001
Location: San Jose, CA USA
Status: Offline
Reply With Quote
Jan 22, 2001, 12:02 AM
 
NSTimers, like the EventLoopTimers in Carbon, are run from the event loop (runloop), so they're limited in accuracy by the length of time that your other event handlers take. If handling a window resize takes 200ms of drawing, no timers will run during that time because the flow of control hasn't returned back to the main runloop.
More accurate timing will require starting another thread and using thread primitives to schedule an exact delay or wakeup time. Unfortunately I know very little about doing that in Cocoa/BSD/Mach, but hopefully this post has been of some use...
     
JBL  (op)
Junior Member
Join Date: Sep 2000
Status: Offline
Reply With Quote
Jan 22, 2001, 01:03 PM
 
I think that is helpful. I am pretty new to this so let me make sure I have it straight. Every thread has its own event loop. The limit on the accuracy comes from the amount of time spent checking other events on the loop (the timer only gets checked once on each pass through the loop so you can't be any more accurate than the maximum amount of time taken to pass through the loop). This seems to mean that a) the simpler the application the more accurate the timing, and b) the fewer events generated by the program, the more accurate the timing.
     
Fresh-Faced Recruit
Join Date: Jan 2001
Location: San Jose, CA USA
Status: Offline
Reply With Quote
Jan 23, 2001, 11:20 PM
 
That's basically correct except that not every thread has its own event loop (runloop). You have to explicitly start a runloop on a new thread if you want one, using CFRunLoop or NSRunLoop. They are very useful for managing inter-thread messaging, but not necessary in all cases.
In your case if you're very concerned about accurate timing, I would simply have a raw thread whose main function uses lower level timing primitives to block until an exact time. (You can tell I'm getting vague here, since I've never done any of this myself. But I'm pretty sure there are POSIX/BSD APIs to do this.)
     
JBL  (op)
Junior Member
Join Date: Sep 2000
Status: Offline
Reply With Quote
Jan 23, 2001, 11:50 PM
 
Thanks! Clearly I have a lot to learn here but I think I know the direction to look at now.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:16 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2