I wonder if accessing the .length instance variable of the array is slower than referencing the local variable you stored it in. I am actually surprised to hear of your results Ralph, but you did the testing so I believe you.
Cheers, Paul On Thu, Sep 25, 2014 at 10:55 AM, Ralph Goers <[email protected]> wrote: > You can think that, but the testing in the testing I did at the time the > difference was quite noticeable. I would have left it as a foreach if it > wasn’t. > > Ralph > > On Sep 25, 2014, at 8:51 AM, Paul Benedict <[email protected]> wrote: > > I would be surprised if foreach over an array makes a speed difference. > AFAIK, foreach is synatic sugar. There is no iterator for an array so it > has to be desugared using a for/index loop like you have there. I don't > think this code is saving anything. > > > Cheers, > Paul > > On Thu, Sep 25, 2014 at 10:47 AM, Remko Popma <[email protected]> > wrote: > >> Hm.. Why did I think it was configuration? I must have gotten mixed up >> with another commit email... >> The class is MarkerManager in log4j-api. >> >> On Fri, Sep 26, 2014 at 12:18 AM, Ralph Goers <[email protected] >> > wrote: >> >>> Configuration? If I recall correctly this method is called on every log >>> event that contains a Marker. But I am just guessing since Gary neglected >>> to say what class this is. But I do remember doing extensive testing when >>> this code was written. And I also remember someone (probably Gary) >>> mentioning then that it should use a for-loop and we had this same >>> conversation then. I think that is why the comment was added. >>> >>> Ralph >>> >>> On Sep 24, 2014, at 9:10 PM, Remko Popma <[email protected]> wrote: >>> >>> >>> On 2014/09/25, at 12:46, Matt Sicker <[email protected]> wrote: >>> >>> From what I remember, it had something to do with the incredibly large >>> difference in speed between for loops and foreach loops on arrays. And by >>> incredibly large, I mean most likely negligible. >>> >>> :-) >>> I do remember reading that someone found a speed difference. But I've >>> never verified it. (Note to self: write a quick jmh benchmark for this.) >>> >>> On the other hand, this is configuration, so it only happens once and is >>> very unlikely to be "hot" code so there is probably not much value in >>> optimizing this loop. >>> >>> >>> On 24 September 2014 22:12, Gary Gregory <[email protected]> wrote: >>> >>>> Why does this "//noinspection ForLoopReplaceableByForEach" comment mean? >>>> >>>> Why not for an enhanced for each loop? >>>> >>>> private static boolean contains(final Marker parent, final Marker... >>>> localParents) { >>>> //noinspection ForLoopReplaceableByForEach >>>> for (int i = 0, localParentsLength = localParents.length; i >>>> < localParentsLength; i++) { >>>> final Marker marker = localParents[i]; >>>> if (marker == parent) { >>>> return true; >>>> } >>>> } >>>> return false; >>>> } >>>> >>>> Thanks, >>>> Gary >>>> >>>> -- >>>> E-Mail: [email protected] | [email protected] >>>> Java Persistence with Hibernate, Second Edition >>>> <http://www.manning.com/bauer3/> >>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/> >>>> Spring Batch in Action <http://www.manning.com/templier/> >>>> Blog: http://garygregory.wordpress.com >>>> Home: http://garygregory.com/ >>>> Tweet! http://twitter.com/GaryGregory >>>> >>> >>> >>> >>> -- >>> Matt Sicker <[email protected]> >>> >>> >>> >> > >
