Note that there are some explanatory texts on larger screens.

plurals
  1. POJava optimization: speed of inner loops inconsistent?
    primarykey
    data
    text
    <p>My friend and I are stumped. In these two blocks of code, why is the first inner loop faster than the second inner loop? Is this some sort of JVM optimization?</p> <pre><code>public class Test { public static void main(String[] args) { int[] arr = new int[100000000]; arr[99999999] = 1; long t1, t2, t3; for (int ndx = 0; ndx &lt; 10; ndx++) { t1 = System.currentTimeMillis(); for (int i = 0; i &lt; arr.length; i++) if (0 &lt; arr[i]) System.out.print(""); t2 = System.currentTimeMillis(); for (int i = 0; i &lt; arr.length; i++) if (arr[i] &gt; 0) System.out.print(""); t3 = System.currentTimeMillis(); System.out.println(t2 - t1 +" "+(t3 - t2)); } } } </code></pre> <h3>And the results:</h3> <pre><code>me@myhost ~ $ java Test 57 80 154 211 150 209 149 209 150 209 150 209 151 209 150 210 150 210 149 209 </code></pre> <h2>Swapped the orderings of inequalities:</h2> <pre><code>public class Test { public static void main(String[] args) { int[] arr = new int[100000000]; arr[99999999] = 1; long t1, t2, t3; for (int ndx = 0; ndx &lt; 10; ndx++) { t1 = System.currentTimeMillis(); for (int i = 0; i &lt; arr.length; i++) if (arr[i] &gt; 0) System.out.print(""); t2 = System.currentTimeMillis(); for (int i = 0; i &lt; arr.length; i++) if (0 &lt; arr[i]) System.out.print(""); t3 = System.currentTimeMillis(); System.out.println((t2 - t1) +" "+(t3 - t2)); } } } </code></pre> <h3>And the results:</h3> <pre><code>me@myhost ~ $ java Test 56 80 155 210 150 209 149 209 151 210 149 209 150 209 149 208 149 209 149 208 </code></pre> <p><em>Insanity: doing the same thing over and over again and getting different results.</em> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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