Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are observing a behavior that is caused by the <strong>optimizations</strong> done by the JIT compiler of your Java VM. This behavior is reproducible triggered with scalar arrays up to 64 elements, and is not triggered with arrays larger than 64.</p> <p>Before going into details, let's take a closer look at the body of the loop:</p> <pre><code>double[] test = new double[64]; </code></pre> <p>The body has no effect <em>(observable behavior)</em>. That means it makes no difference outside of the program execution whether this statement is executed or not. The same is true for the whole loop. So it might happen, that the code optimizer translates the loop to something <em>(or nothing)</em> with the same functional and different timing behavior.</p> <p>For benchmarks you should at least adhere to the following two guidelines. If you had done so, the difference would have been significantly smaller.</p> <ul> <li>Warm-up the JIT compiler (and optimizer) by executing the benchmark several times.</li> <li>Use the result of every expression and print it at the end of the benchmark. </li> </ul> <hr> <p>Now let's go into details. Not surprisingly there is an optimization that is triggered for scalar arrays not larger than 64 elements. The optimization is part of the <a href="http://en.wikipedia.org/wiki/Escape_analysis">Escape analysis</a>. It puts small objects and small arrays onto the stack instead of allocating them on the heap - or even better optimize them away entirely. You can find some information about it in the following article by Brian Goetz written in 2005:</p> <ul> <li><a href="http://www.ibm.com/developerworks/java/library/j-jtp09275/index.html">Urban performance legends, revisited: Allocation is faster than you think, and getting faster</a></li> </ul> <p>The optimization can be disabled with the command line option <code>-XX:-DoEscapeAnalysis</code>. The magic value 64 for scalar arrays can also be changed on the command line. If you execute your program as follows, there will be no difference between arrays with 64 and 65 elements:</p> <pre><code>java -XX:EliminateAllocationArraySizeLimit=65 Tests </code></pre> <p>Having said that, I strongly discourage using such command line options. I doubt that it makes a huge difference in a realistic application. I would only use it, if I would be absolutely convinced of the necessity - and not based on the results of some pseudo benchmarks.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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