I want to calculate the average time taken to call a method, but the results are very much dependent on the "numberOfTries" variable in the code below:
int averageExecutionTime = 0;
int numberOfTries = someNumber;
for(int i=0 ; i<numberOfTries ; ++i){
long t = System.nanoTime();
CallSomeMethod();
averageExecutionTime += (System.nanoTime()-t);
}
averageExecutionTime /= numberOfTries;
System.out.println(averageExecutionTime);
When I run my method for the first time, it takes about 6000 nanoseconds to call the method. However, when I call it again, the time taken reduces to about 50 nanoseconds. What is the cause of this reduction?