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.