title: What is going on in this java benchmark?
body: <p>I'm using junit to run a primitive benchmark like this:</p>
  <pre><code>@Test
  public void testTime() throws Exception{
      LoadoutBase&lt;?&gt; loadout = new LoadoutStandard(&quot;AS7-D-DC&quot;);
  
      final int iterations = 1000000;
  
      long start = System.nanoTime();
      int sum = 0;
      for(int i = 0; i &lt; iterations; ++i){
          Iterator&lt;Item&gt; it = loadout.iterator();
          while( it.hasNext() ){
              sum += it.next().getNumCriticalSlots();
          }
      }
      long end = System.nanoTime();
  
      long time_it = end - start;
  
      start = System.nanoTime();
      sum = 0;
      for(int i = 0; i &lt; iterations; ++i){
          for(Item item : loadout.getAllItems()){
              sum += item.getNumCriticalSlots();
          }
      }
      end = System.nanoTime();
  
      long time_arrays = end - start;
    
      System.out.println(&quot;it: &quot; + time_it + &quot; array: &quot; + time_arrays + &quot; diff: &quot; + (double)time_it/time_arrays);
  }
  </code></pre>
  <p>If I set <code>iterations=1000000</code> then I get</p>
  <blockquote>
  <p>it: 792771504 array: 1109215387 diff: 0.7147137637029551</p>
  </blockquote>
  <p>very consistently but if I set <code>iterations=10000</code> then I get</p>
  <blockquote>
  <p>it: 32365742 array: 28902811 diff: 1.1198129482976587</p>
  </blockquote>
  <p>with very wild fluctuations. The diff parameters is anywhere from 0.7 to 1.2</p>
  <p>Could any one shed some light on what might be happening here? Which method should I choose?</p>
  <h2>Edit:</h2>
  <p>What I really am benchmarking is behind the scenes work. <code>getAllItems</code> creates a new <code>List&lt;Item&gt;</code> and populates it by getting all items from 16 sublists using <code>addAll</code>. The <code>Iterator</code> approach doesn't construct this temporary list but rather keeps track of in which of these 16 sublists it is currently iterating and has some logic to make the 16 sublists look like a continuous list.</p>