Even thought it's not its main purpose, I've always thought that the final
keyword (in some situations and VM implementations) could help the JIT.
It might be an urban legend but I've never imagined that setting a field final
could negatively affect the performances.
Until I ran into some code like that:
private static final int THRESHOLD = 10_000_000;
private static int [] myArray = new int [THRESHOLD];
public static void main(String... args) {
final long begin = System.currentTimeMillis();
//Playing with myArray
int index1,index2;
for(index1 = THRESHOLD - 1; index1 > 1; index1--)
myArray[index1] = 42; //Array initial data
for(index1 = THRESHOLD - 1; index1 > 1; index1--) {
//Filling the array
for(index2 = index1 << 1; index2 < THRESHOLD; index2 += index1)
myArray[index2] += 32;
}
long result = 0;
for(index1 = THRESHOLD - 1; index1 > 1; index1-=100)
result += myArray[index1];
//Stop playing, let's see how long it took
System.out.println(result);
System.out.println((System.currentTimeMillis())-begin+"ms");
}
Let's have a look at:
private static int [] myArray = new int [THRESHOLD];
Under W7 64-bit and on a basis of 10 successive runs, I get the following results:
THRESHOLD = 10^7
, 1.7.0u09 client VM (Oracle):- runs in ~2133ms when
myArray
is not final. - runs in ~2287ms when
myArray
is final. - The -server VM produces similar figures i.e 2131ms and 2284ms.
- runs in ~2133ms when
THRESHOLD = 3x10^7
, 1.7.0u09 client VM (Oracle):- runs in ~7647ms when
myArray
is not final. - runs in ~8190ms when
myArray
is final. - The -server VM produces ~7653ms and ~8150ms.
- runs in ~7647ms when
THRESHOLD = 3x10^7
, 1.7.0u01 client VM (Oracle):- runs in ~8166ms when
myArray
is not final. - runs in ~9694ms when
myArray
is final. That's more than 15% difference ! - The -server VM produces a neglectable difference in favour of the non-final version, about 1%.
- runs in ~8166ms when
Remark: I used the bytecode produced by JDK 1.7.0u09's javac for all my tests. The bytecode produced is exactly the same for both versions except for myArray
declaration, that was expected.
So why is the version with a static final myArray
slower than the one with static myArray
?
EDIT (using Aubin's version of my snippet):
It appears that the differences between the version with final
keyword and the one without only lies in the first iteration. Somehow, the version with final
is always slower than its counterpart without on the first iteration, then next iterations have similar timings.
For example, with THRESHOLD = 10^8
and running with 1.7.0u09 client the first computation takes approx 35s while the second 'only' takes 30s.
Obviously the VM performed an optimization, was that the JIT in action and why didn't it kick earlier (for example by compiling the second level of the nested loop, this part was the hotspot) ?
Note that my remarks are still valid with 1.7.0u01 client VM. With that very version (and maybe earlier releases), the code with final myArray
runs slower than the one without this keyword: 2671ms vs 2331ms on a basis of 200 iterations.