Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: high accuracy timing options?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: high accuracy timing options?

At 11:23 AM -0700 3/27/08, Aaron Turner wrote:
Anyways, I'm going to try to dig around some more and try to find the
HPET timer API.  Theoretically, that should be more accurate then what
I'm seeing with gtod().  I saw Steve's MPDelayUntil() comment, but it
makes no indication what the method is,

Huh? I posted links to the documentation and sample code, which I assumed you could read.

You calculate a time in the future in AbsoluteTime units and call MPDelayUntil(), which delays until that time is past.

and it seems unlikely that it
would be any better then this.  I'm guessing it's just a wrapper
around nanosleep().

You would be wrong. It's a high-performance, high-accuracy call from the original Mac OS Driver Services library that was moved up to Multiprocessor Sevices library. It does not rely on converting unix time units to processor time units.

The example I pointed you to:


does exactly what what you want. Note that SubAbsoluteFromAbsolute() returns 0, not a negative number if the time has passed.

Note this code is written in Eudora, not tested.

From the example I gave you (to convert back to seconds):

static float HowLong(
    AbsoluteTime endTime,
    AbsoluteTime bgnTime

    AbsoluteTime absTime;

    Nanoseconds  nanosec;

absTime = SubAbsoluteFromAbsolute(endTime, bgnTime);

    nanosec = AbsoluteToNanoseconds(absTime);

    return (float) UnsignedWideToUInt64( nanosec ) / 1000.0;


main(int argc, char *argv[])
AbsoluteTime fiveSeconds = DurationToAbsolute(5000 * kDurationMillisecond);
AbsoluteTime now = UpTime();
AbsoluteTime last = now;
AbsoluteTime sleep_until = now;

    u_int32_t count = 0, fail = 0, back = 0, i;
    struct timeval sleep_until, now, last;

for (i = 0; i < 5; i ++) {
sleep_until = AddAbsoluteToAbsolute(UpTime(), fiveSeconds); // what you wrote
// sleep_until = AddAbsoluteToAbsolute(sleep_until, fiveSeconds); // this won't drift

   //     MPDelayUntil(&sleep_until); // would just until 5 sec from start
        last = UpTime();

        do {
            now         = UpTime();
            if (now == last)
                fail ++;
            else if (SubAbsoluteFromAbsolute(now, endTime) == 0)
                back ++;

            last = now;
        } while (timercmp(&now, &sleep_until, <));

        printf("Ran gtod() %lu times in 5 seconds.\n", count);
        printf("%f gtod()/usec\n", (float)count / 5000 / 1000);
        printf("Failed to increment %lu times\n", fail);
        printf("Back in time %lu times\n", back);
        count = 0;
        fail = 0;
        back = 0;

Note that AbsoluteTime may be declared as a struct, but you can cast to UInt64.

What are you really trying to do?

Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

 >high accuracy timing options? (From: "Aaron Turner" <email@hidden>)
 >Re: high accuracy timing options? (From: Andrew Gallatin <email@hidden>)
 >Re: high accuracy timing options? (From: "Aaron Turner" <email@hidden>)

Visit the Apple Store online or at retail locations.

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.