3

Im doing some performance optimising on some code, and I've got two ways of doing a calculation.

I've got a simple test harness:

    long start, end;
    long oldDuration, newDuration;
    int answer;

    final int TEST_COUNT = 1000;

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

    newDuration = end - start;


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

    oldDuration = end - start;

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

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

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
  • 1
    This isn't a great harness. I would suggest running them both multiple times in an infinite loop and then disregard the first 10 or so runs. It takes a while to stabilize and for everything to be hotspot compiled, etc. You should never be looking at the first set of results. Just manually kill the test when it looks like it's stabilized. – Mark Peters Nov 17 '10 at 14:57

3 Answers3

2

Add answer to some sum variable and, on test's exit, print that sum.

Victor Sorokin
  • 11,878
  • 2
  • 35
  • 51
2

Simply keep adding the result to your dummy result variable, i.e.

answer += doCaluculationNewWay()

Then print the end result to the console - then even the most aggressively optimizing JIT should consider the results to be actually used.

And if that addition has any impact at all on the total running time, then I very much doubt you should be worried about the performance in the first place.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • Exactly what I would have suggested. You can use answer much later, after both tests have run and been recorded. – Mark Peters Nov 17 '10 at 14:55
1

If you are trying to do a microbenchmark, be careful. In addition to what the guys said above (keep track of the total and print it), be sure to use the right tool. Something like Google Caliper is good for micro benchmarks.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
bwawok
  • 14,898
  • 7
  • 32
  • 43