View Full Version : I need a high precision timer (cocoa)
rasteblaster
2004.07.16, 12:41 AM
I'm new to MacOSX programming.
I need to get a high precision clock to use with my game engine.
Something that gives me the time in milliseconds.
Any help would be greatly appreciated,
chris
OneSadCookie
2004.07.16, 01:13 AM
+[NSDate timeIntervalSinceReferenceDate]
Returns a double, 1.0 == 1 second, accurate to well below milliseconds.
imported_kelvin
2004.07.16, 01:21 AM
NSDate *reftime = [NSDate date];
double howmanysecondselapsed = [[NSDate date] timeIntervalSinceDate:reftime];
slightly easier to manage.
belthaczar
2004.07.17, 03:56 PM
NSDate *reftime = [NSDate date];
double howmanysecondselapsed = [[NSDate date] timeIntervalSinceDate:reftime];
Umm, wow, that looks incredibly easy compared to what I had been using:
UnsignedWide currTime = {0, 0};
Microseconds(&currTime);
double time = UnsignedWideToUInt64(currTime) / 1000000.0;
I don't suppose anyone knows why I would have used such an ugly piece of code when there was a nice solution in Cocoa?
--phillip
imported_kelvin
2004.07.17, 07:41 PM
90% of learning Cocoa is figuring out what classes do what. I find most common things are not obvious until you've done them once or twice.
rasteblaster
2004.07.18, 03:23 PM
Thanks to everybody for the quick replies!
arekkusu
2004.07.18, 06:07 PM
Milliseconds are not really high precision. At 60fps you have a 16 ms window, a 1 ms error is enough to make your fps counter wrong.
gettimeofday() will give you time in microseconds.
UpTime() will give you time in nanoseconds (obviously not at 1 ns precision...)
ThemsAllTook
2004.07.18, 08:42 PM
Unless I'm greatly mistaken, UpTime() gives you microseconds, not nanoseconds...
Alex Diener
imported_kelvin
2004.07.18, 09:27 PM
NSTimeInterval = double = 8bytes = 64bits
From the documentation, The NSTimeInterval type makes possible a wide and fine-grained range of date and time values, giving accuracy within milliseconds for dates 10,000 years apart
10k years = ~315360000000 seconds.
divide by 64bits = 0.000 000 01709 seconds
sub-microsecond accuracy, probably even after you account for the floating point bits.
I'd say NSDate is plenty accurate for any needs.
OneSadCookie
2004.07.18, 10:36 PM
sub-microsecond precision, not accuracy :)
A double only has 53 significant bits.
arekkusu
2004.07.18, 11:03 PM
UpTime() returns the value right from the PPC tb register, which you then convert into nanoseconds. I'm pretty sure [NSDate timeIntervalSinceReferenceDate] is doing exactly the same thing but converting to floating point seconds. No problem there, I'm just responding to the initial question with the observation that measuring in milliseconds (i.e. glutGet(GLUT_ELAPSED_TIME)) isn't good enough.
imported_kelvin
2004.07.18, 11:12 PM
ok with 52 significant bits (http://www.freesoft.org/CIE/RFC/1832/10.htm)... that gives... 0.000 070 seconds precision. Still fine for most uses.
500fps only needs 0.002 seconds.
arekkusu
2004.07.18, 11:17 PM
Getting off topic now but what if you want to do more than post a fps number? Say, measure how much each bit of your rendering is taking per frame? :) I.e. how much time building objects, rendering audio, submitting vertices, etc. There's a definite need for sub-millisecond precision. Look at MAME's profiling.
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.