Best way of stopping java optimising away a method call when performance testing
<p>Im doing some performance optimising on some code, and I've got two ways of doing a calculation.</p>

<p>I've got a simple test harness:</p>

<pre><code>    long start, end;
    long oldDuration, newDuration;
    int answer;

    final int TEST_COUNT = 1000;

    start = System.currentTimeMillis();
    for(int i = 0 ; i &lt; TEST_COUNT; i++)
    {
        answer = doCaluculationNewWay()
    }
    end = System.currentTimeMillis();

    newDuration = end - start;


    start = System.currentTimeMillis();
    for(int i = 0 ; i &lt; TEST_COUNT; i++)
    {
        answer = doCaluculationOldWay()
    }
    end = System.currentTimeMillis();

    oldDuration = end - start;
</code></pre>

<p>How can I be sure that the JVM isnt just binning the call, knowing that the answer isnt being used?</p>

<p>I considered making answer volatile, but Im worried that it will impose an additional performance overhead due to memory synching issues.</p>
