I found that first call of some function take too long time. Here my simple test:
public class MainTest {
public static void main(String[] args) {
long k = 0;
for (int i = 0; i < 10; i++) {
long start = System.nanoTime();
k += doWork(i);
System.out.println(System.nanoTime() - start);
}
System.out.println(k);
}
public static long doWork(long var) {
for (int i = 0; i < 100000; i++) {
var += i;
}
return var;
}
}
Results:
820521
283961
292514
259442
88952
86100
82965
81539
74126
29651
Can you give me some versions about why it's happening? Or some resources where I can find the answer. It can be connected with JIT, but I'am not sure about this.
I know that nanotime is not correct for testing. I try JMH:
# Warmup Iteration 1: 179791124,395 ops/s
# Warmup Iteration 2: 183962412,435 ops/s
# Warmup Iteration 3: 284320650,805 ops/s