Re: mach_absolute_time() vs. sleep() [solved]
Re: mach_absolute_time() vs. sleep() [solved]
- Subject: Re: mach_absolute_time() vs. sleep() [solved]
- From: Terry Lambert <email@hidden>
- Date: Tue, 6 May 2008 12:16:58 -0700
On May 1, 2008, at 1:55 PM, Steve Checkoway wrote:
So let's consider the three cases above:
1. info.denom / 1e9 * info.numer has units of mt * ns / 1e9 = mt * s.
2. 1e9 * info.numer / info.denom has units of 1e9 * ns / mt = as /
mt (that is attoseconds per mach time or 10^-18 seconds per mach time)
3. 1e-9 * info.numer / info.denom has units of 1e-9 * ns / mt = s/mt.
Your original (i.e., number 3) was correct. I'm not sure exactly
what Terry was talking about when he was saying you were making
mistakes. The following code is a complete working example. The
numbers I get are pretty close to being the number of seconds asleep.
Conversion to lvalue types happens before the operation is completed.
The order of the operators in C is such that the multiply happens
before the divide. If that happens, and your time base happens to be
close (the closer to exactly 1GHz, the more likely this is to be an
issue), then you've effectively reduce the value below the resolution
of the significance of the digits in the mantissa. When that happens,
rounding gets to be very important, and tiny rounding issues get
magnified.
If I had been assured this wasn't homework, I would have pointed this
out. Moving the 1e-9 multiply to the next line ensures the order of
the divide and the "multiply" (divide by 1e9) avoid losing significant
digits.
Amusingly, I've seen this problem before internally; usually it's
someone trying to second-guess the clock; I say amusingly because all
our clock timers are done on the basis of real intervals, not lbolt
values, so in something like a sleep(6);, we don't have the +/- 1
second rounding error you might see on other platforms.
-- Terry
_______________________________________________
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