I’m writing a method to compute the running time for an algorithm. The algorithm is run a specified number of trials and then I compute the running time using System.currentTimeMillis(). However whenever I call the method the value always ends up being zero. Any direction towards fixing this problem is appreciated.
public long runTime(algorithm alg, int[] array, int trials)
{
long initialTime = System.currentTimeMillis();
for(int i = 0; i < trials; i++)
{
alg.runAlgorithm(array);
}
return ((System.currentTimeMillis() - initialTime) / (long) trials);
}
((System.currentTimeMillis() - initialTime) / (long) trials);It seems the Algorithm is taking less than a
1 msto complete its loop, and so you need to break it even to smaller unit… UsingnanoTime()may do the work for u…Eg: