I was looking at a project in java and found a for loop which was written like below:
for(int i=1; i
For your convenience, I've microbenchmarked it. The code:
public class ArrayLength
{
static final boolean[] ary = new boolean[10_000_000];
static final Random rnd = new Random();
@GenerateMicroBenchmark public void everyTime() {
int sum = rnd.nextInt();
for (int i = 0; i < ary.length; i++) sum += sum;
}
@GenerateMicroBenchmark public void justOnce() {
int sum = rnd.nextInt();
final int length = ary.length;
for (int i = 0; i < length; i++) sum += sum;
}
}
The results:
Benchmark Mode Thr Cnt Sec Mean Mean error Units
o.s.ArrayLength.everyTime thrpt 1 3 5 40215.790 1490.800 ops/msec
o.s.ArrayLength.justOnce thrpt 1 3 5 40231.192 966.007 ops/msec
Summary: no detectable change.