title: Why is processing a sorted array *slower* than an unsorted array? (Java's ArrayList.indexOf)
body: <p>The title is in reference to <a href="https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array">Why is it faster to process a sorted array than an unsorted array?</a></p>
  
  <p>Is this a branch prediction effect, too? Beware: here the processing for the sorted array is <em>slower</em>!!</p>
  
  <p>Consider the following code:</p>
  
  <pre><code>private static final int LIST_LENGTH = 1000 * 1000;
  private static final long SLOW_ITERATION_MILLIS = 1000L * 10L;
  
  @Test
  public void testBinarySearch() {
      Random r = new Random(0);
      List&lt;Double&gt; list = new ArrayList&lt;&gt;(LIST_LENGTH);
      for (int i = 0; i &lt; LIST_LENGTH; i++) {
          list.add(r.nextDouble());
      }
      //Collections.sort(list);
      // remove possible artifacts due to the sorting call
      // and rebuild the list from scratch:
      list = new ArrayList&lt;&gt;(list);
  
      int nIterations = 0;
      long startTime = System.currentTimeMillis();
      do {
          int index = r.nextInt(LIST_LENGTH);
          assertEquals(index, list.indexOf(list.get(index)));
          nIterations++;
      } while (System.currentTimeMillis() &lt; startTime + SLOW_ITERATION_MILLIS);
      long duration = System.currentTimeMillis() - startTime;
      double slowFindsPerSec = (double) nIterations / duration * 1000;
      System.out.println(slowFindsPerSec);
  
      ...
  }
  </code></pre>
  
  <p>This prints out a value of around 720 on my machine.</p>
  
  <p>Now if I activate the collections sort call, that value drops down to 142. Why?!?</p>
  
  <p>The results <em>are</em> conclusive, they don't change if I increase the number of iterations/time.</p>
  
  <p>Java version is 1.8.0_71 (Oracle VM, 64 bit), running under Windows 10, JUnit test in Eclipse Mars.</p>
  
  <p><em>UPDATE</em></p>
  
  <p>Seems to be related to contiguous memory access (Double objects accessed in sequential order vs in random order). The effect starts vanish for me for array lengths of around 10k and less.</p>
  
  <p>Thanks to assylias for providing <a href="https://bitbucket.org/assylias/performance/src/master/src/main/java/com/assylias/performance/SO35018999.java?at=master&amp;fileviewer=file-view-default" rel="noreferrer">the results</a>:</p>
  
  <pre><code>/**
   * Benchmark                     Mode  Cnt  Score   Error  Units
   * SO35018999.shuffled           avgt   10  8.895 ± 1.534  ms/op
   * SO35018999.sorted             avgt   10  8.093 ± 3.093  ms/op
   * SO35018999.sorted_contiguous  avgt   10  1.665 ± 0.397  ms/op
   * SO35018999.unsorted           avgt   10  2.700 ± 0.302  ms/op
   */
  </code></pre>