Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

re: exact time



On Thu, 28 Aug 2003 11:00:24 -0500, Bob Sabiston <email@hidden>
wrote:
> Hello,
> What is the best way to get the exact time, quickly? I am running
> into problems timing things by ticks, because 1/60 second is not fast
> enough. Is there a better way that is fast? I have been using
> TickCount(), and I read that GetDateTime is not very exact.

The absolutely highest resolution clock on a Macintosh is always UpTime().
It moves the (64-bit) bus clock register directly into two 32-bit registers
(or one 64-bit register on a G5?). It has virtually no overhead.

While we're on the subject, a common novice mistake is to convert to a
standard timebase as you go:

[BEGIN]
double seconds(void)
{
return AbsoluteToNanoSeconds(UpTime()) * 0.000000001f;
}

double start = seconds();

// stuff to be timed
.
.
.

double stop = seconds();

double delta = stop - start;

[END]

This is bad because it includes the absolute to seconds convertion time as
part of what we're timing. Instead grab the start/stop UpTimes and then
convert them after the timing is complete:

[BEGIN]

AbsoluteTime start = UpTime();

// stuff to be timed
.
.
.

AbsoluteTime stop = UpTime();

AbsoluteTime delta = stop - start;

double seconds = AbsoluteToNanoSeconds(delta) * 0.000000001f;

[END]

--
Enjoy,
George Warner,
Schizophrenic Optimization Scientists
Apple Developer Technical Support (DTS)
_______________________________________________
carbon-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/carbon-development
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.