title: Elapsed time using System.nanoTime() around several method calls delivers a lower value than the sum of of the elapsed times in each method?
body: <p>I'm using System.nanoTime() to measure the time it takes to call several methods.
  In each of those methods I do the same to measure how long each method takes.
  In the end the sum of the elapsed times should be smaller than the total elapsed time, or so I thought. However, it isn't.</p>
  
  <p>Example:</p>
  
  <pre><code>public static void main(String[] args){
    long startTime = System.nanoTime();
    method1();
    method2();
    method3();
    System.out.println( "Total Time: " + (System.nanoTime() - startTime) / 1000000);
  }
  private void method1(){
    long startTime = System.nanoTime();
    //Stuff
    System.out.println( "Method 1: " + (System.nanoTime() - startTime) / 1000000);
  }
  // same for all other methods
  </code></pre>
  
  <p>In my case I get something around 950ms for the total time, but the sum of each elapsed time is 1300ms+. Why is this?</p>
  
  <p>EDIT:</p>
  
  <p>Okay, to be a bit clearer, I am <em>not</em> getting this behaviour when writing to arrays many times (which I just did as a test). When I do this, I get pretty much exact results (+-1ms).</p>
  
  <p>What I am actually doing is this:</p>
  
  <p>I read two pretty huge text files into String arrays (1000 * ~2000 characters in first file, 200 * ~100 characters in the second).</p>
  
  <p>I then do a whole lot of comparisons on the String array I got from reading the first file and use the results to calculate some probabilities.</p>
  
  <p>EDIT2: Error on my part, I was calling methods within methods and summed up those times as well, which were already included. Without these double-times it all adds up. Thanks for clearing this up!</p>