Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You haven't provided enough information to allow for any definitive answers, but the jvm runtime optimizer is extremely powerful and does all sorts of inlining, runtime dataflow and escape analysis, and all manner of cache tricks.</p> <p>The end result is to make the sort of micro-benchmarks you are trying to perform all but useless in practice; and extremely difficult to get right even when they are potentially useful.</p> <p>Definitely read <a href="http://www.ibm.com/developerworks/java/library/j-benchmark1.html" rel="nofollow noreferrer">http://www.ibm.com/developerworks/java/library/j-benchmark1.html</a> for a fuller discussion on the problems you face. At the very least you need to ensure:</p> <ol> <li>foo is called in a loop that runs thousands of times</li> <li>foo() returns a result, and</li> <li>that result is used</li> </ol> <p>The following is the minimum starting point, assuming foo() is non-trivial and therefore is unlikely to be inlined. Note: You still have to expect loop-unrolling and other cache level optimizations. Also watch out for the hotspot compile breakpoint (I believe this is ~5000 calls on -server IIRC), which can completely stuff up your measurements if you try to re-run the measurements in the same JVM.</p> <pre><code>public class FooBar { public static void main(String[] args) { int sum = 0; int ITERATIONS = 10000; for (int i = 0; i &lt; ITERATIONS; i++) { sum += foo(i); } System.out.println("%d iterations returned %d sum", ITERATIONS, sum); } } </code></pre> <p>Seriously, you need to do some reading before you can make any meaningful progress towards writing benchmarks on a modern JVM. The same optimizations that allows modern Java code to match or even sometimes beat C++ make benchmarking really difficult.</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