I think Effective Java is just incorrect on this point. I have some benchmarks that show a clear difference.
The difference is more pronounced when iterating over small collections many times, since a new Iterator object must be instantiated for each iteration. The compiler seems to handle a lot of smallish collections. Dan On Thu, Jun 4, 2009 at 3:47 PM, Aaron Steele <[email protected]> wrote: > > So item 46 in Effective Java says that there shouldn't be a > performance penalty using the nice for loops. But the following test > in Eclipse on my machine (MacBook Pro, Intel Core Duo, 2.16 GHz) shows > a performance penalty. > > Given an ArrayList called ints with 1 million Integers, this takes 31 > milliseconds: > for (int i = 0, size = ints.size(); i < size; i++) > ints.get(i).intValue(); > > And this takes 76 milliseconds: > for (Integer i : ints) > i.intValue(); > > What am I missing? Probably just some super naive testing on my part. :) > > On Thu, Jun 4, 2009 at 12:32 PM, Daniel Rice (דניאל רייס) > <[email protected]> wrote: >> >> The speedup was measured over 3 non-JProfiler runs, invoked within >> Eclipse. It's certainly possible that some of the difference is >> normal runtime fluctuation but each optimized run was faster than any >> of the non-optimized runs. >> >> Dan >> >> On Thu, Jun 4, 2009 at 3:28 PM, Scott Blum <[email protected]> wrote: >>> On Thu, Jun 4, 2009 at 2:54 PM, Lex Spoon <[email protected]> wrote: >>>> >>>> The original posts says a 3% improvement. I have found subsequent >>>> runs of the compiler to vary by larger than this amount, but let's >>>> assume this is the average improvement over many runs. >>> >>> The 3% is as measured by something like JProfiler, or that's some kind of >>> absolute timer? I've found JProfiler sometimes overreports time spent in >>> small/fast methods that are invoked many times, but when I measure with no >>> profiling and using something like unix time, the differences disappear. >>> Does the 3% hold up without -soyc enabled? >>> >> >> > >> > > > > -- > > Sent from Piedmont, CA, United States > > > > --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---
