Re: high accuracy timing options?
site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com Here's a working version with numbers for a MacBook Pro 2.16GHz: -------- #include <CoreServices/CoreServices.h> #include <sys/time.h> #define NonZero(x) ((x).hi | (x).lo) int main(int argc, char *argv[]) { u_int32_t count = 0, fail = 0, back = 0, i; AbsoluteTime sleep_until, now, last, time_left; AbsoluteTime fiveSeconds = DurationToAbsolute(5000 * kDurationMillisecond); } while (NonZero(time_left)); printf("Ran UpTime() %lu times in 5 seconds.\n", count); printf("%f UpTime()/usec\n", (float)count / 5000 / 1000); printf("Failed to increment %lu times\n", fail); printf("Back in time %lu times\n", back); printf("-------------\n"); count = 0; fail = 0; back = 0; } exit(0); } Ran UpTime() 54272359 times in 5 seconds. 10.854471 UpTime()/usec Failed to increment 0 times Back in time 0 times ------------- Ran UpTime() 54571267 times in 5 seconds. 10.914254 UpTime()/usec Failed to increment 0 times Back in time 0 times ------------- Ran UpTime() 55075625 times in 5 seconds. 11.015125 UpTime()/usec Failed to increment 0 times Back in time 0 times ------------- Ran UpTime() 55023597 times in 5 seconds. 11.004719 UpTime()/usec Failed to increment 0 times Back in time 0 times ------------- Ran UpTime() 55205105 times in 5 seconds. 11.041020 UpTime()/usec Failed to increment 0 times Back in time 0 times ------------- TimerTest has exited with status 0. _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... At 3:14 PM -0400 3/27/08, Steve Sisak wrote: Note this code is written in Eudora, not tested. Still not sure what you're trying to do, but this is the same as your code minus "back", which can't happen -- hope this tells you something. Since UpTime() just returns the processor timebase register, I'm pretty sure it can't fail to increment. Note that the absolute value of fiveSeconds is going to vary based on processor architecture and speed. for (i = 0; i < 5; i ++) { time_left = fiveSeconds; // make non-zero for fail case sleep_until = AddAbsoluteToAbsolute(UpTime(), fiveSeconds); last = UpTime(); do { count++; now = UpTime(); if ((now.lo == last.lo) && (now.hi == last.hi)) { fail ++; } else { time_left = SubAbsoluteFromAbsolute(sleep_until, now); } last = now; This email sent to site_archiver@lists.apple.com
participants (1)
-
Steve Sisak