Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Everything you need is described in <a href="http://developer.apple.com/library/mac/qa/qa1398/">Technical Q&amp;A QA1398: Technical Q&amp;A QA1398: Mach Absolute Time Units</a>, basically the function you want is <code>mach_absolute_time</code>.</p> <p>Here's a slightly earlier version of the sample code from that page that does everything using Mach calls (the current version uses <code>AbsoluteToNanoseconds</code> from CoreServices). In current OS X (i.e., on Snow Leopard on x86_64) the absolute time values are actually in nanoseconds and so don't actually require any conversion at all. So, if you're good and writing portable code, you'll convert, but if you're just doing something quick and dirty for yourself, you needn't bother.</p> <p>FWIW, <code>mach_absolute_time</code> is <em>really</em> fast.</p> <pre><code>uint64_t GetPIDTimeInNanoseconds(void) { uint64_t start; uint64_t end; uint64_t elapsed; uint64_t elapsedNano; static mach_timebase_info_data_t sTimebaseInfo; // Start the clock. start = mach_absolute_time(); // Call getpid. This will produce inaccurate results because // we're only making a single system call. For more accurate // results you should call getpid multiple times and average // the results. (void) getpid(); // Stop the clock. end = mach_absolute_time(); // Calculate the duration. elapsed = end - start; // Convert to nanoseconds. // If this is the first time we've run, get the timebase. // We can use denom == 0 to indicate that sTimebaseInfo is // uninitialised because it makes no sense to have a zero // denominator is a fraction. if ( sTimebaseInfo.denom == 0 ) { (void) mach_timebase_info(&amp;sTimebaseInfo); } // Do the maths. We hope that the multiplication doesn't // overflow; the price you pay for working in fixed point. elapsedNano = elapsed * sTimebaseInfo.numer / sTimebaseInfo.denom; printf("multiplier %u / %u\n", sTimebaseInfo.numer, sTimebaseInfo.denom); return elapsedNano; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload