title: Measuring time in Java
body: <p>So I was trying to measure the time two different algorithm implementations took to accomplish a given task, and here is the result:</p>
  
  <pre><code>i    alg1  alg2
  4   0.002   0.0
  5   0.001   0.0
  6   0.003   0.002
  7   0.023   0.01
  8   0.055   0.041
  9   0.056   0.0
  10  0.208   0.101
  11  1.767   0.694
  12  18.581  7.784
  </code></pre>
  
  <p>being <code>i</code> just some input parameter.</p>
  
  <p>I've measured the performance of the algorithms making use of the following (naive) function:</p>
  
  <pre><code>private double getDuration() {
      return (double)(System.currentTimeMillis() - startTime) / (double)1000;
  }
  </code></pre>
  
  <p>What would be the preferable way of getting more real results (other than 0.0, which obviously isn't true!) than using <code>System.currentTimeMillis()</code>? I know I could just run the algorithms over and over again and sum their results, but I have this gut feeling that there is probably some more robust way of measuring passed time in Java (both <code>real</code>, <code>user</code> and <code>sys</code>, if possible!).</p>
  
  <p>Thanks</p>