Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had a bit of time over lunch:</p> <pre><code>public static void main(String[] args) { final int[] a = new int[250000000]; long t; for (int j = 0; j &lt; 10; j++) { t = System.currentTimeMillis(); for (int i = 0, n = a.length; i &lt; n; i++) { int x = a[i]; } System.out.println("n = a.length: " + (System.currentTimeMillis() - t)); t = System.currentTimeMillis(); for (int i = 0; i &lt; a.length; i++) { int x = a[i]; } System.out.println("i &lt; a.length: " + (System.currentTimeMillis() - t)); } } </code></pre> <p>The results:</p> <pre><code>n = a.length: 672 i &lt; a.length: 516 n = a.length: 640 i &lt; a.length: 516 n = a.length: 656 i &lt; a.length: 516 n = a.length: 656 i &lt; a.length: 516 n = a.length: 640 i &lt; a.length: 532 n = a.length: 640 i &lt; a.length: 531 n = a.length: 641 i &lt; a.length: 516 n = a.length: 656 i &lt; a.length: 531 n = a.length: 656 i &lt; a.length: 516 n = a.length: 656 i &lt; a.length: 516 </code></pre> <p>Notes:</p> <ol> <li>If you reverse the tests, then <code>n = a.length</code> shows as being faster than <code>i &lt; a.length</code> by about half, probably due to garbage collection(?).</li> <li>I couldn't make <code>250000000</code> much larger because I got <code>OutOfMemoryError</code> at <code>270000000</code>.</li> </ol> <p>The point is, and it is the one everyone else has been making, you have to run Java out of memory and you still don't see a significant difference in speed between the two alternatives. Spend your development time on things that actually matter.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload