Re: NSTimer: Test Results
Re: NSTimer: Test Results
- Subject: Re: NSTimer: Test Results
- From: Andreas Grosam <email@hidden>
- Date: Mon, 16 Feb 2009 20:39:08 +0100
On Feb 16, 2009, at 4:02 PM, Andreas Grosam wrote:
On Feb 16, 2009, at 3:13 PM, Jean-Daniel Dupas wrote:
Le 16 févr. 09 à 13:44, Andreas Grosam a écrit :
Hello,
how does a NSTimer object that has been setup with a repeating
time interval calculate the time when it fires an event?
There may be two possibilities:
Say, the initial time is at t0, the interval is T, and the time
when it fires the first event is at t1, the second at t2, etc.
The time when it fires the ith event will be ti:
A)
ti = t (i-1) + T
B)
ti = i * T + t0
Reason:
If I need a "tick" that corresponds to a current time, I would
prefer method B (mean error remains stable).
In case A the firing time may fade away with respect to the
initial time due to inherent inaccuracies. Although this error is
certainly small compared to the error that we have when we measure
the time when we actually do handle the event, but it may
accumulate over a long duration. That is the error increases with
time.
The class overview in the NSTimer reference page adresses this
topic in a detailed way.
A repeating timer reschedules itself based on the scheduled firing
time, not the actual firing time. For example, if a timer is
scheduled to fire at a particular time and every 5 seconds after
that, the scheduled firing time will always fall on the original 5
second time intervals, even if the actual firing time gets delayed.
If the firing time is delayed so far that it passes one or more of
the scheduled firing times, the timer is fired only once for that
time period; the timer is then rescheduled, after firing, for the
next scheduled firing time in the future.
Thank you all,
this is the paragraph that answers my question. Sorry, I read the
documentation, but as frequently, I tend to skip exactly those
paragraphs which I am looking for ;)
So, according the documentation, it is neither method A or method B
which is used to calculate the firing time. Actually it is:
Let ts(i) be the scheduling times, where ts(0) is the initial
scheduled time, T is the interval and i refers to the ith timer event,
i >= 1.
ts(i) = ts(i-1) + T
I expected that the scheduling time has a very small inaccuracy,
however a quick test shows that the error is quite large:
For T = 1 sec, I got an offset of -1 msec roughly every 25 events.
This will accumulate to 150msec after an hour.
This large error cannot be explained due to rounding errors or due to
precision limits using doubles as the time value. I measured the time
with [NSDate timeIntervalSinceReferenceDate].
So, where does the error come from?
Interestingly, I was able to compensate the error by setting the timer
interval to 1.0 + 0.001/25.
After using this interval, the measured error almost disappeared, and
the events pretty exactly got fired every second.
I set up the timer as follows:
// Setup a fireDate that fires on trunc(now + 1.5):
NSTimeInterval nowDuration = [NSDate
timeIntervalSinceReferenceDate];
NSTimeInterval fireDuration = trunc(nowDuration + 1.5);
NSDate *fireDate = [NSDate
dateWithTimeIntervalSinceReferenceDate:fireDuration];
NSTimer* timer = [[NSTimer alloc] initWithFireDate:fireDate
interval:tickInterval
target:self
selector:@selector(tickHandler:)
userInfo:[self userInfo]
repeats:YES];
Regards
Andreas
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden