lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert engels <>
Subject Re: [jira] Created: (LUCENE-1257) Port to Java5
Date Fri, 11 Apr 2008 12:50:39 GMT
When iterating over an array using an indexed loop, you typically  
need to access the element, as follows:

for(int i=0;i<100;i++) {
	String s = array[i];

Java performs bounds checking on the array[i] access to make sure i  
is within the limits of the array. Granted, there are optimizations  
the JVM can do in many cases using escape processing to know that i  
will always be in the range, but it is not always feasible.

when you use

for(String s : array) {

the JVM uses its own internal indexer that it knows cannot be outside  
the bounds, and thus the bounds checking can be avoided.

I would need to read the spec to know what happens if the array  
changes during the loop execution - my bet is that the loop maintains  
a reference to the original array, and thus it continues to work.

On Apr 11, 2008, at 2:28 AM, Endre StĂžlsvik wrote:

> robert engels wrote:
>> The 'foreach' should be faster in the general case for arrays as  
>> the bounds checking can be avoided.
> Why is that? Where do you mean that the bounds-checking can be  
> avoided?
>> But, I doubt the speed difference is going to matter much either  
>> way, and eventually the JVM impl will converge to near equal  
>> performance.
> This I actually agree on: if the foreach has some disadvantage of  
> explicit indexing through an array, it will at some point be fixed  
> so that it doesn't have this disadvantage anymore..
> Endre.
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message