title: Measuring elapsed CPU time in Julia
body: <p>Many scientific computing languages make a distinction between absolute time (wall clock) and CPU time (processor cycles). For example, in Matlab we have:</p>
  
  <pre><code>&gt;&gt; tic; pause(1); toc
  Elapsed time is 1.009068 seconds.
  
  &gt;&gt; start = cputime; pause(1); elapsed = cputime - start
  elapsed =
          0
  </code></pre>
  
  <p>and in Mathematica we have:</p>
  
  <pre><code>&gt;&gt;In[1]:= AbsoluteTiming[Pause[1]]
  &gt;&gt;Out[1]= {1.0010572, Null}
  
  &gt;&gt;In[2]:= Timing[Pause[1]]
  &gt;&gt;Out[2]= {0., Null}
  </code></pre>
  
  <p>This distinction is useful when benchmarking code run on computation servers, where there may be high variance in the absolute timing results depending on what other processes are running concurrently.</p>
  
  <p>The Julia standard library provides support for timing of expressions through <code>tic()</code>, <code>toc()</code>, <code>@time</code> and a few other functions/macros all based on <code>time_ns()</code>, a function that measures absolute time.</p>
  
  <pre><code>&gt;&gt;julia&gt; @time sleep(1)
  elapsed time: 1.017056895 seconds (135788 bytes allocated)
  </code></pre>
  
  <p>My question: Is there a simple way to get the elapsed CPU time for an expression evaluation in Julia?</p>
  
  <p>(Side note: doing some digging, it appears that Julia timing is based on the <code>uv_hrtime()</code> function from <a href="https://github.com/joyent/libuv">libuv</a>. It seems to me that using <code>uv_getrusage</code> from the same library might give a way to access elapsed CPU time in Julia, but I'm no expert. Has anybody tried using anything like this?)</p>