• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
accurate timing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

accurate timing


  • Subject: accurate timing
  • From: Michael Willhide <email@hidden>
  • Date: Sat, 5 Apr 2003 16:37:56 -0700

Hi,

I know there have been several posts previously on how to use timers and such for animation. I have more questions about using NSTimeInterval and the NSDate timeIntervalSinceReferenceDate. All the documentation I've read says that NSTimeInterval is a double that reports time in seconds. However when I display the interval it appears to be on the order or milliseconds, but not quite. I wrote the following code in a simple foundation tool to test some things out.

<code>

NSTimeInterval i1;
NSTimeInterval i2;
NSTimeInterval i3;
NSTimeInterval totalTime;
NSTimeInterval start;
NSTimeInterval stop;

int i,j;

start = [NSDate timeIntervalSinceReferenceDate];

for(i=0; i<1000; i++)
{
i1 = [NSDate timeIntervalSinceReferenceDate];

//do something so the intervals aren't too close...
for( j=0; j<100; j++)
{
;
}

i2 = [NSDate timeIntervalSinceReferenceDate];

i3 = (i2-i1)*1000; //multiply by 1000 since the intervals appear to be reported close to milliseconds

totalTime += i3;

NSLog(@"framerate = %f\t\tTotal Time = %f", i3, totalTime);

i++;
}

stop = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"Total Run Time = %f", stop-start);

</code>

And I get output that looks something like below.. I only included the last few lines.

<output>
...
framerate = 0.002757 Total Time = 2.160802
framerate = 0.002593 Total Time = 2.163395
framerate = 0.002816 Total Time = 2.166212
framerate = 0.006974 Total Time = 2.173185
Total Run Time = 3.207824

</output>

The discrepancy between the Total Time reported by accumulating the deltas and the total reported by the difference between start and stop just increases with the number of iterations.

Is there a more accurate way to do timing? The "cocoa programming" book (anguish, buck, yacktman) says the timeIntervalSinceReferenceDate is accurate to milliseconds, and that NSTimeInterval is stored in seconds. The latter is obviously not the case unless there is another way you're supposed to format the answer.

Thanks for the help!

BTW: i'm working on a physics based application and i'd like the results to be accurate.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
  • Follow-Ups:
    • Re: accurate timing
      • From: John Anderson <email@hidden>
  • Prev by Date: Re: Showing NSView's contextual menu on mouse down
  • Next by Date: Re: gcc pragma pack() question
  • Previous by thread: Re: Showing NSView's contextual menu on mouse down
  • Next by thread: Re: accurate timing
  • Index(es):
    • Date
    • Thread