site_archiver@lists.apple.com Delivered-To: Darwin-dev@lists.apple.com User-agent: Thunderbird 2.0.0.12 (X11/20080213) Kristopher Matthews wrote:
conversion = 1e9 * info.numer / info.denom; conversion = 1e-9 * (double) info.numer / (double) info.denom;
} Before that, you were dividing the numerator by (the denominator * 1e9). #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 (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... conversion = info.denom / 1e9 * info.numer; Now you're multiplying the numerator and the denominator and dividing by 1000000000. Before, you were multiplying the numerator by 1e9 and dividing by the denominator. 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. This email sent to site_archiver@lists.apple.com