It depends on the cost of .size(), it is done on each run of the body
and cannot be optimized by the compiler (.size() could change during
the loop).

A common pattern is to do:
for (int i = 0, len = x.size(); i < len; ++i) {
   ...
}

Peter


On Fri, Feb 4, 2011 at 2:03 PM, Antoine Levy-Lambert <anto...@gmx.de> wrote:
>
> Hello Stefan,
>
> 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.
> Interesting.
>
> I just read the original bug report "Project.fireMessageLoggedEvent
> performance fix" [1] and the one you have addressed [2].
>
> In an ideal world, should the compiler not do these optimizations for us
> automatically ?
>
> Regards,
>
> Antoine
>
> [1] https://issues.apache.org/bugzilla/show_bug.cgi?id=19101
> [2] https://issues.apache.org/bugzilla/show_bug.cgi?id=50716
>
>
> On 2/3/2011 4:00 PM, bode...@apache.org wrote:
>>
>> Author: bodewig
>> Date: Thu Feb  3 21:00:00 2011
>> New Revision: 1066963
>>
>> URL: http://svn.apache.org/viewvc?rev=1066963&view=rev
>> Log:
>> microoptimizations.  PR 50716
>>
>> Modified:
>>     ant/core/trunk/src/main/org/apache/tools/ant/IntrospectionHelper.java
>>     ant/core/trunk/src/main/org/apache/tools/ant/Main.java
>>     ant/core/trunk/src/main/org/apache/tools/ant/Target.java
>>     ant/core/trunk/src/main/org/apache/tools/ant/UnknownElement.java
>
> .....
>>
>> URL:
>> http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/mail/MailMessage.java?rev=1066963&r1=1066962&r2=1066963&view=diff
>>
>> ==============================================================================
>> --- ant/core/trunk/src/main/org/apache/tools/mail/MailMessage.java
>> (original)
>> +++ ant/core/trunk/src/main/org/apache/tools/mail/MailMessage.java Thu Feb
>>  3 21:00:00 2011
>> @@ -328,7 +328,8 @@ public class MailMessage {
>>      //   "Header fields are NOT required to occur in any particular
>> order,
>>      //    except that the message body MUST occur AFTER the headers"
>>      // (the same section specifies a reccommended order, which we ignore)
>> -   for (int i = 0; i<  headersKeys.size(); i++) {
>> +   final int size = headersKeys.size();
>> +   for (int i = 0; i<  size; i++) {
>>        String name = (String) headersKeys.elementAt(i);
>>        String value = (String) headersValues.elementAt(i);
>>        out.println(name + ": " + value);
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@ant.apache.org
> For additional commands, e-mail: dev-h...@ant.apache.org
>
>

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

Reply via email to