I'm attempting to profile some methods to see which implementations are faster. I've run into a snag as java appears to be cheating the profiles by not processing all the information, each time it's requested.
public void Profile() {
double[] testArray = new double[100000000];
Math math = new Math();
long start = System.nanoTime();
for (int i = 1; i <= 100000000; i++) {
testArray[i - 1] = math.Divide(i);
}
long stop = System.nanoTime();
System.out.println("math.divide(): " + TimeUnit.NANOSECONDS.toMillis(stop - start));
System.out.println(testArray[(int) (java.lang.Math.random() * (100000000 - 0))]);
}
public class Math {
public double Divide(int i) {
int dividend = i;
int divisor = 12;
return dividend / (double) divisor;
}
}
if I don't assign the the Math.Divide() value to the array, the entire for loop returns in 2 ms, when I do, it returns in ~200 ms, which still seems too fast (considering an identical implementation in C# takes a minimum of 1.8 seconds).
ultimately my questions are: is there a more accurate way to profile a method? and how can I override java so that it stops skipping work it considers redundant.