On Wed, Sep 5, 2012 at 12:32 PM, Gregory Collins <greg@gregorycollins.net> wrote:
On recent (>2009) Linux, you can call clock_gettime() with CLOCK_REALTIME_COARSE which is about 6x faster:

https://gist.github.com/3634708

On my workstation calling clock_gettime(CLOCK_REALTIME, ...) and poking the value into a Haskell struct takes about 70ns, with CLOCK_REALTIME_COARSE it's about 19ns (with a much lower stddev). Worth using for this application if it's available. Of course, in Snap we do the "update the clock every second in a thread" trick also.

Of course, I made a rookie mistake here, and can't reproduce the results because a single call is too short to benchmark accurately. Adding a replicateM_ 1000 gives:

CLOCK_PROCESS_CPUTIME_ID: 195.0717 ns
CLOCK_REALTIME: 24.92609 ns
CLOCK_REALTIME_COARSE: 14.47882 ns
CLOCK_MONOTONIC: 25.00275 ns
CLOCK_MONOTONIC_COARSE: 15.60754 ns

Updated the gist accordingly.

G
--
Gregory Collins <greg@gregorycollins.net>