Hi,

>> On 02/04/2011 09:03 AM, Antoine Levy-Lambert wrote:
>>> I did not know that using a final variable for the upper bound of a loop 
>>> instead of something.size() makes a difference in terms of performance.
>
>> Generally the preferred idiom is to use an iterator,
>
> Absolutely agreed, but that would have been a bigger change so I shied
> away from it (as well as from replacing Vectors with some other lists in
> various places).

Yeah the idiom I use is:

for(int i=0, len=list.size(); i<len; i++)

This is good as instead of calling the .size() method on every
execution of the loop it's essentially caching the value and using
that.  Secondly, the scope of the additional variable is limited to
just the loop.  Having the len variable defined outside the loop means
that you have something that is only for the loop existing beyond the
lifetime of the loop (until the return from the method).

But on the other hand defining the variable as final is probably a win
too as the complier will just replace it with a constant int.

In general though iterators are supposed to offer the 'best'
performance as they are micro optimized for the particular collection.
 On the other hand an iterator is another level of indirection :)

Interesting discussion - I tried to introduce some micro-optimizations
(pretty much like these) into the Tomcat codebase 8 years ago, but was
met with 'meh why bother?' responses as the thinking was that the vm
would pretty much make optimizations pointless.  I suppose it depends
on where the hotspots are.

Kev

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@ant.apache.org
For additional commands, e-mail: dev-h...@ant.apache.org

Reply via email to