Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: high precision timer
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: high precision timer



> I have written a small test app to test the speed and accuracy of
> gettimeofday(), UpTime(), and GetEventTime(). Drop me a mail if interested.
>
> Bottom line is that gettimeofday() seems to be the fastest (on some machines),
> but only has 1ms (iirc) resolution, while UpTime() is not much slower than
> gettimeofday(), but has a higher resolution, on my machine the resolution is
> in the range of the time it takes the function to execute.

You have a bug in your code. There's NWIH gettimeofday could be faster than
UpTime. Uptime is the highest resolution (64-bits) timer on any PowerPC
processor. It counts bus cycles (usually 1/4 the CPU speed) since the last
reset (or (re?)boot). All other system timers and clocks are based off of
this counter (including gettimeofday).

// This is the source for mach_absolute_time (from Darwin):
uint64_t mach_absolute_time(void)
{
__asm__ volatile("0: mftbu r3");
__asm__ volatile("mftb r4");
__asm__ volatile("mftbu r0");
__asm__ volatile("cmpw r0,r3");
__asm__ volatile("bne- 0b");
}

UpTime calls this and returns the 64-bit value; gettimeofday calls this,
converts the absolute time to micro-seconds and then adds that to the base
time when the system was last (re?)booted.

There are routines to convert this value to different units of seconds.

BTW: if you want to time an operation:

// DON'T DO THIS!
NanoSeconds startTime = AbsoluteToNanoseconds(UpTime());

// operations to be timed...
.
.
.
NanoSeconds stopTime = AbsoluteToNanoseconds(UpTime());
NanoSeconds deltaTime = stopTime - startTime;

// This will include the AbsoluteToNanoseconds conversion in the timing.

// Instead:
AbsoluteTime startTime = UpTime();

// operations to be timed...
.
.
.
AbsoluteTime stopTime = UpTime();
NanoSeconds stopTime = AbsoluteDeltaToNanoseconds(stopTime, startTime);

This will minimalize the calling and conversion overhead.

If you look in <mach/mach_time.h> you'll see these two functions:

uint64_t mach_absolute_time(void);
kern_return_t mach_wait_until( uint64_t deadline);

These are the mach version of the Carbon API's UpTime and MPDelayUntil. ;-)

The mach_timebase_info API returns the conversion info necessary to convert
between absolute times and conventional time units (seconds). For example,
to convert an absolute time to nanoseconds:

mach_timebase_info_data_t tTBI;

mach_timebase_info(&tTBI);
cv = ((long double) tbinfo.numer) / ((long double) tbinfo.denom);

uint64_t now = mach_absolute_time();
Nanoseconds tNS = now * cv;
tNS += 10000000; // 10 milliSeconds
uint64_t then = tNS / cv; // convert back to machine time

mach_wait_until(then);

--
Enjoy,
George Warner,
Schizophrenic Optimization Scientists
Apple Developer Technical Support (DTS)
_______________________________________________
mac-games-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/mac-games-dev
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 © 2011 Apple Inc. All rights reserved.