Re: mach_absolute_time() vs. sleep() [solved]
Re: mach_absolute_time() vs. sleep() [solved]
- Subject: Re: mach_absolute_time() vs. sleep() [solved]
- From: Kristopher Matthews <email@hidden>
- Date: Thu, 1 May 2008 16:19:47 -0500
Thank you.
Can anyone point me to any documentation for mach_timebase_info() and/
or mach_absolute_time() ? I've googled, I've searched help in Xcode,
and I've tried man pages.
--Kris
On May 1, 2008, at 3:55 PM, Steve Checkoway wrote:
Kristopher Matthews wrote:
conversion = info.denom / 1e9 * info.numer;
Now you're multiplying the numerator and the denominator and
dividing by 1000000000.
> conversion = 1e9 * info.numer / info.denom;
Before, you were multiplying the numerator by 1e9 and dividing by
the denominator.
> conversion = 1e-9 * (double) info.numer / (double)
info.denom;
}
Before that, you were dividing the numerator by (the denominator *
1e9).
Think about what units you want here. Your mach_elapsed_time() is
returning a value in seconds. mach_absolute_time() gives you a time
in units of mt (mach time, a unit I just made up). You can consider
info.numer as having units of ns--nanoseconds--and info.denom as
having units of mt. Since you want seconds in the end, your
conversion had better have units of s/mt.
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.
I just checked the link you gave and it almost exactly the same as
this except that info.numer is being multiplied by 1e-9 before being
divided by info.denom.
#include <mach/mach_time.h>
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static double mach_elapsed_time( uint64_t start, uint64_t end )
{
static double conversion = 0.;
if( conversion == 0. )
{
mach_timebase_info_data_t info;
mach_timebase_info( &info );
conversion = info.numer / (1e9 * info.denom);
}
return (end - start)*conversion;
}
int main( int argc, const char *argv[] )
{
uint64_t start = mach_absolute_time();
uint64_t end;
int sleepTime = argc == 2? atoi(argv[1]):3;
sleep( sleepTime );
end = mach_absolute_time();
printf( "%f seconds\n", mach_elapsed_time(start, end) );
return 0;
}
--
Steve Checkoway
_______________________________________________
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
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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