title: Measure time in Linux - time vs clock vs getrusage vs clock_gettime vs gettimeofday vs timespec_get?
body: <p>Among the timing functions, <code>time</code>, <code>clock</code> <code>getrusage</code>, <code>clock_gettime</code>, <code>gettimeofday</code> and <code>timespec_get</code>, I want to understand clearly how they are implemented and what are their return values in order to know in which situation I have to use them.</p>
  
  <p>First we need to classify functions returning <strong>wall-clock values</strong> compare to functions returning <strong>process or threads values</strong>. <code>gettimeofday</code> returns wall-clock value, <code>clock_gettime</code> returns wall-clock value <strong>or</strong> process or threads values depending on the <code>Clock</code> parameter passed to it. <code>getrusage</code> and <code>clock</code> return process values.</p>
  
  <p>Then the second question regards the implementation of these functions and as a consequence, their accuracy. Which hardware or software mechanism does these functions use. </p>
  
  <p>It seems that <code>getrusage</code> uses only the kernel tick (usually 1ms long) and as a consequence can't be more accurate than the ms. Is it right? 
  Then the <code>getimeofday</code> function seems to use the most accurate underlying hardware available. As a consequence its accuracy is usually the microsecond (can't be more because of the API) on recent hardware. 
  What about <code>clock</code>, the man page speak about "approximation", what does it mean? 
  What about <code>clock_gettime</code>, the API is in nanosecond, does it means that it's able to be so accurate if underlying hardware allows it? What about monotonicity?</p>
  
  <p>Are there any other functions?</p>