3

I'm trying to understand what is the best way to sample timestamps in a Mac OS X 64 bit environment, using the gcc compiler. I read about the TSC register in x86 architectures and HPET for Intel processors, but I can't find a guide to use them. Actually, I tried with the function gettimeofday() but I need the precision of nanosecond.

Can anyone lead me?

nneonneo
  • 171,345
  • 36
  • 312
  • 383
camillo_benso
  • 107
  • 2
  • 2
  • 7
  • 1
    http://stackoverflow.com/a/8583395/318716 – Joseph Quinsey Oct 25 '12 at 02:17
  • http://stackoverflow.com/a/3051474/318716 – Joseph Quinsey Oct 25 '12 at 02:19
  • 1
    You will be _incredibly_ unlikely to get a resolution/granularity anywhere near a nanosend. Even those functions that state they return nanoseconds tend to have a much higher granularity (meaning for example it may jump from 0ns to 4000ns without any intervening values). I'd be _very_ interested in why you think you need that sort of granularity. – paxdiablo Oct 25 '12 at 02:20
  • Why do you think you need nanosecond resolution of time? I work on delivering real time market updates for 100's of exchanges - we use usec and we cant get most stuff below 20usec – Adrian Cornish Oct 25 '12 at 02:23

1 Answers1

4

On OS X, you can use the mach_absolute_time function to get a high-precision timestamp:

#include <mach/mach_time.h>
#include <stdint.h>

/* get timer units */
mach_timebase_info_data_t info;
mach_timebase_info(&info);

/* get timer value */
uint64_t ts = mach_absolute_time();

/* convert to nanoseconds */
ts *= info.numer;
ts /= info.denom;

Note that if you are trying to time something, you should perform the final nanosecond conversion on the difference between timestamps (the duration) to avoid overflow problems.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • And what is the resolution of the time returned by mach_absolute_time() – Adrian Cornish Oct 25 '12 at 03:03
  • On my machine, it's 1 nanosecond (`info.numer` and `info.denom` are both equal to 1). The difference between adjacent calls is about 30ns as reported by the function. – nneonneo Oct 25 '12 at 03:10
  • I asked for the spec of the resolution of the timer not the value – Adrian Cornish Oct 25 '12 at 03:19
  • Like many other timer functions, the resolution is platform-dependent. I can't give you a specification. But, it's no reason to downvote the answer. – nneonneo Oct 25 '12 at 03:24
  • See [What is `mach_absolute_time()` based on?](http://stackoverflow.com/questions/1450737/what-is-mach-absolute-time-based-on-on-iphone). See also [Quick Performance Measurements](http://iosdeveloperzone.com/2011/05/03/quick-performance-measurements/). – Jonathan Leffler Oct 25 '12 at 03:49
  • @nneonneo It is a perfectly reasonable to downvote your answer. Because it is just wrong. a) you cannot guarantee that resolution on a Mac b) you cannot guarantee is on any other platform. Read the OP's question "but I need the precision of nanosecond." - show me how you provide that – Adrian Cornish Oct 25 '12 at 03:59
  • I *never said* it provides nanosecond accuracy. It's the best timer offered by Mach (the OS X kernel), and on most Macs it does happen to be a nanosecond timer. If you think you can do better, answer the question. – nneonneo Oct 25 '12 at 04:03