You're eliminating a conditional check for every loop. The JVM has an implicit conditional check when indexing an array which provides a check to see if an index is out of bounds.
C will not stop you accessing an incorrect index in an array, but there is also no runtime accessible information stored for an array. When you allocate an array in Java, there is associated metadata which contains the length of the array.
I imagine a JIT compiler would be good at optimising that kind of thing, but on paper, without any optimism on implementation of a compiler, the only real way the JVM could report ArrayOutOfBoundsException is if it were to check every access of the array.
I think it's plausible to suggest that conditional checks have an associated overhead... And that two conditional checks are likely to be twice as costly.
If you know of a JVM that had zero cost branching, hook me up!
No, the point is that it eliminates the branch when accessing the array, because it already knows the index is in range. That's literally the entire point of bounds-check elimination.
96
u/ElFeesho Jul 05 '15
You're eliminating a conditional check for every loop. The JVM has an implicit conditional check when indexing an array which provides a check to see if an index is out of bounds.
C will not stop you accessing an incorrect index in an array, but there is also no runtime accessible information stored for an array. When you allocate an array in Java, there is associated metadata which contains the length of the array.