On Wed, Sep 5, 2012 at 12:32 PM, Gregory Collins <span dir="ltr">&lt;<a href="mailto:greg@gregorycollins.net" target="_blank">greg@gregorycollins.net</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On recent (&gt;2009) Linux, you can call clock_gettime() with CLOCK_REALTIME_COARSE which is about 6x faster:<div><br></div><div><a href="https://gist.github.com/3634708" target="_blank">https://gist.github.com/3634708</a></div>
<div><br></div>
<div>On my workstation calling clock_gettime(CLOCK_REALTIME, ...) and poking the value into a Haskell struct takes about 70ns, with CLOCK_REALTIME_COARSE it&#39;s about 19ns (with a much lower stddev). Worth using for this application if it&#39;s available. Of course, in Snap we do the &quot;update the clock every second in a thread&quot; trick also.</div>

<div></div></blockquote></div><div><br></div><div>Of course, I made a rookie mistake here, and can&#39;t reproduce the results because a single call is too short to benchmark accurately. Adding a replicateM_ 1000 gives:</div>
<div><br></div><div>CLOCK_PROCESS_CPUTIME_ID: 195.0717 ns</div><div>CLOCK_REALTIME: 24.92609 ns</div><div>CLOCK_REALTIME_COARSE: 14.47882 ns</div><div>CLOCK_MONOTONIC: 25.00275 ns</div><div>CLOCK_MONOTONIC_COARSE: 15.60754 ns</div>
<div><br></div><div>Updated the gist accordingly.</div><div><br></div><div>G</div>-- <br>Gregory Collins &lt;<a href="mailto:greg@gregorycollins.net" target="_blank">greg@gregorycollins.net</a>&gt;<br>