Return-Path: X-Original-To: apmail-logging-log4j-dev-archive@www.apache.org Delivered-To: apmail-logging-log4j-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AC90E17689 for ; Fri, 26 Sep 2014 14:28:13 +0000 (UTC) Received: (qmail 25144 invoked by uid 500); 26 Sep 2014 14:28:13 -0000 Delivered-To: apmail-logging-log4j-dev-archive@logging.apache.org Received: (qmail 25094 invoked by uid 500); 26 Sep 2014 14:28:13 -0000 Mailing-List: contact log4j-dev-help@logging.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Log4J Developers List" Reply-To: "Log4J Developers List" Delivered-To: mailing list log4j-dev@logging.apache.org Received: (qmail 25083 invoked by uid 99); 26 Sep 2014 14:28:13 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 26 Sep 2014 14:28:13 +0000 X-ASF-Spam-Status: No, hits=2.5 required=5.0 tests=FREEMAIL_REPLY,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of remko.popma@gmail.com designates 209.85.217.170 as permitted sender) Received: from [209.85.217.170] (HELO mail-lb0-f170.google.com) (209.85.217.170) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 26 Sep 2014 14:28:09 +0000 Received: by mail-lb0-f170.google.com with SMTP id n15so2880270lbi.1 for ; Fri, 26 Sep 2014 07:27:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=GpGktcDMlJYH5h+K7t5mhe/EYPN3LBEzr0/I/FKVpAo=; b=J4X9w43M3ew/MRG9vXyJ21spZb29pKmt+5FWzmX7+o8Qza4YO3liTKgDYhQVkKLgXu e5dXINXOGHsILLMCdwUKSyNf7eZwAqiBPxdj/PrSDSk0sBUWNRCAtcYLvGmop+s+0Hv3 OOXWWcX7Z5PRLsqUQqwUBmUU4GIISdMknPUVgghqFVAGYGKuzN/HSFaxJ0OEQ/zSHCjz 0tW0uc2kEjbTGT7AGf8eLWTlqUhdEej44/IQZXLScTaJA9dG6sIETt0qH4ue55l58pK2 XryIalthvX3Aer4z+zdAZd17lTrgymvc5p+zNf4FuLUA2s12e9wcjB7Iz+0Pte8hTRWA cfHw== MIME-Version: 1.0 X-Received: by 10.112.142.104 with SMTP id rv8mr18960534lbb.59.1411741667591; Fri, 26 Sep 2014 07:27:47 -0700 (PDT) Received: by 10.152.186.197 with HTTP; Fri, 26 Sep 2014 07:27:47 -0700 (PDT) In-Reply-To: References: <9D348A16-A49D-4C38-81B0-6E66B86DCB07@gmail.com> <58EE9B4F-B192-493F-90DE-E2DD7C329CB5@dslextreme.com> Date: Fri, 26 Sep 2014 23:27:47 +0900 Message-ID: Subject: Re: No for each loop comment? From: Remko Popma To: Log4J Developers List Content-Type: multipart/alternative; boundary=001a11c3783e9c8a1b0503f8b8c9 X-Virus-Checked: Checked by ClamAV on apache.org --001a11c3783e9c8a1b0503f8b8c9 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Nope. Maybe I'll get around that next week. If you have time to do that, please share! On Fri, Sep 26, 2014 at 11:23 PM, Mikael St=C3=A5ldal < mikael.staldal@appearnetworks.com> wrote: > Have you compared the generated byte code (using javap -c) for the two > cases? > > On Fri, Sep 26, 2014 at 3:43 PM, Remko Popma > wrote: > >> On Windows it looks like normal for loops are slightly faster than >> for-each loops, especially for small arrays of primitives. This could be >> noise, since we are talking about 5 nanoseconds where the baseline (an >> empty method invocation) is 12 nanos. >> >> On Solaris 10 and Red Hat Enterprise Linux the baseline is so large (255 >> nanos and 1910 nanos respectively) that any difference we are seeing is >> just noise. >> >> All benchmarks were run with one fork, one thread, 10 warmup iterations >> and 10 test iterations. >> >> *Windows 7 (64bit) with Java 1.8.0_05, 2-core Intel i5-3317u CPU @1.70Gh= z >> with hyperthreading switched on (4 virtual cores)* >> Benchmark Mode Samples >> Score Score error Units >> o.a.l.l.p.j.LoopsBenchmark.baseline sample 154947 >> 12.432 0.550 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10000ForEachLoop sample 126597 >> 2759.592 3.431 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10000ForLoop sample 126494 >> 2761.729 3.127 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray1000ForEachLoop sample 154124 >> 292.880 1.065 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray1000ForLoop sample 156155 >> 288.751 1.101 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray100ForEachLoop sample 191980 >> 41.826 0.870 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray100ForLoop sample 193770 >> 36.894 0.782 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10ForEachLoop sample 190847 >> 22.393 0.618 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10ForLoop sample 192552 >> 17.146 0.560 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10000ForEachLoop sample 173839 >> 31959.057 14.341 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10000ForLoop sample 171137 >> 32461.985 14.353 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray1000ForEachLoop sample 97495 >> 3591.200 4.852 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray1000ForLoop sample 101560 >> 3445.998 4.010 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray100ForEachLoop sample 102796 >> 438.207 1.923 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray100ForLoop sample 102333 >> 439.576 2.139 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10ForEachLoop sample 113924 >> 58.957 1.247 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10ForLoop sample 120416 >> 60.712 1.284 ns/op >> >> >> // For loops for Object arrays are similar but return the total XOR of >> the element hashcodes. >> >> private int forEachLoop(final int[] array) { >> int result =3D 0; >> for (final int element : array) { >> result ^=3D element; >> } >> return result; >> } >> >> private int forLoop(final int[] array) { >> int result =3D 0; >> for (int i =3D 0; i < array.length; i++) { >> result ^=3D array[i]; >> } >> return result; >> } >> >> >> >> *Solaris 10 (64bit) with JDK1.7.0_06-b24 (Oracle Hotspot), 2 quad-core >> Xeon X5570 dual CPUs @2.93Ghz with hyperthreading switched on (16 virtua= l >> cores)* >> Benchmark Mode Samples >> Score Score error Units >> o.a.l.l.p.j.LoopsBenchmark.baseline sample 110212 >> 255.300 0.201 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10000ForEachLoop sample 187808 >> 3938.055 1.207 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10000ForLoop sample 187897 >> 3937.929 0.748 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray1000ForEachLoop sample 123989 >> 606.631 0.626 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray1000ForLoop sample 123973 >> 609.565 0.416 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray100ForEachLoop sample 126933 >> 294.204 0.280 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray100ForLoop sample 127070 >> 296.411 0.223 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10ForEachLoop sample 113400 >> 261.519 0.181 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10ForLoop sample 111637 >> 260.435 0.115 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10000ForEachLoop sample 115872 >> 48154.673 18.846 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10000ForLoop sample 116777 >> 47793.868 17.615 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray1000ForEachLoop sample 138432 >> 5256.767 2.451 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray1000ForLoop sample 136644 >> 5325.377 2.388 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray100ForEachLoop sample 166653 >> 773.541 0.330 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray100ForLoop sample 166570 >> 774.513 0.574 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10ForEachLoop sample 178754 >> 317.232 0.134 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10ForLoop sample 180165 >> 316.189 0.238 ns/op >> >> *64 bit RHEL 6.5 (Linux 2.6.32-431.el6.x86_64) with JDK1.7.0_05-b06 >> (Oracle Hotspot), 4 quad-core Xeon X5570 CPUs @2.93GHz with hyperthreadi= ng >> switched on (16 virtual cores)* >> Benchmark Mode Samples >> Score Score error Units >> o.a.l.l.p.j.LoopsBenchmark.baseline sample 114783 >> 1910.576 29.256 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10000ForEachLoop sample 194584 >> 5132.885 25.137 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10000ForLoop sample 196672 >> 4811.572 52.072 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray1000ForEachLoop sample 133119 >> 1967.213 28.970 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray1000ForLoop sample 133804 >> 2004.501 31.554 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray100ForEachLoop sample 142439 >> 1575.329 6.457 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray100ForLoop sample 142215 >> 1957.714 27.815 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10ForEachLoop sample 130826 >> 1980.301 30.818 ns/op >> o.a.l.l.p.j.LoopsBenchmark.intArray10ForLoop sample 132654 >> 1589.120 8.449 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10000ForEachLoop sample 126947 >> 43301.320 50.589 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10000ForLoop sample 126117 >> 43574.129 55.272 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray1000ForEachLoop sample 143697 >> 5831.250 19.667 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray1000ForLoop sample 163244 >> 4823.096 13.180 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray100ForEachLoop sample 162502 >> 1930.819 24.136 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray100ForLoop sample 171619 >> 1625.806 10.385 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10ForEachLoop sample 172226 >> 1888.683 22.554 ns/op >> o.a.l.l.p.j.LoopsBenchmark.objArray10ForLoop sample 188838 >> 1581.979 6.322 ns/op >> >> >> >> >> On Fri, Sep 26, 2014 at 2:56 AM, Matt Sicker wrote: >> >>> The foreach over an array looks like it's supposed to compile to the >>> same thing: >>> >>> https://jcp.org/aboutJava/communityprocess/jsr/tiger/enhanced-for.html >>> >>> Same goes for .length which is supposed to be a final field which would >>> allow for inlining by the JIT I'd imagine (hence why we use final >>> everywhere): >>> >>> http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7 >>> >>> On 25 September 2014 11:33, Gary Gregory wrote= : >>> >>>> On Thu, Sep 25, 2014 at 11:55 AM, Ralph Goers < >>>> ralph.goers@dslextreme.com> 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 forea= ch if >>>>> it wasn=E2=80=99t. >>>>> >>>>> Ralph >>>>> >>>>> On Sep 25, 2014, at 8:51 AM, Paul Benedict >>>>> 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 >>>>> 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 < >>>>>> ralph.goers@dslextreme.com> wrote: >>>>>> >>>>>>> Configuration? If I recall correctly this method is called on ever= y >>>>>>> log event that contains a Marker. But I am just guessing since Gar= y >>>>>>> neglected to say what class this is. But I do remember doing exten= sive >>>>>>> testing when this code was written. And I also remember someone (p= robably >>>>>>> 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 >>>>>>> wrote: >>>>>>> >>>>>>> >>>>>>> On 2014/09/25, at 12:46, Matt Sicker wrote: >>>>>>> >>>>>>> From what I remember, it had something to do with the incredibly >>>>>>> large difference in speed between for loops and foreach loops on ar= rays. >>>>>>> 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.) >>>>>>> >>>>>>> >>>> I'd be curious to see the results! >>>> >>>> Gary >>>> >>>> >>>>> >>>>>>> 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 >>>>>>> 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 =3D 0, localParentsLength =3D >>>>>>>> localParents.length; i < localParentsLength; i++) { >>>>>>>> final Marker marker =3D localParents[i]; >>>>>>>> if (marker =3D=3D parent) { >>>>>>>> return true; >>>>>>>> } >>>>>>>> } >>>>>>>> return false; >>>>>>>> } >>>>>>>> >>>>>>>> Thanks, >>>>>>>> Gary >>>>>>>> >>>>>>>> -- >>>>>>>> E-Mail: garydgregory@gmail.com | ggregory@apache.org >>>>>>>> Java Persistence with Hibernate, Second Edition >>>>>>>> >>>>>>>> JUnit in Action, Second Edition >>>>>>>> Spring Batch in Action >>>>>>>> Blog: http://garygregory.wordpress.com >>>>>>>> Home: http://garygregory.com/ >>>>>>>> Tweet! http://twitter.com/GaryGregory >>>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>>>> -- >>>>>>> Matt Sicker >>>>>>> >>>>>>> >>>>>>> >>>>>> >>>>> >>>>> >>>> >>>> >>>> -- >>>> E-Mail: garydgregory@gmail.com | ggregory@apache.org >>>> Java Persistence with Hibernate, Second Edition >>>> >>>> JUnit in Action, Second Edition >>>> Spring Batch in Action >>>> Blog: http://garygregory.wordpress.com >>>> Home: http://garygregory.com/ >>>> Tweet! http://twitter.com/GaryGregory >>>> >>> >>> >>> >>> -- >>> Matt Sicker >>> >> >> > > > -- > Mikael St=C3=A5ldal > Chief Software Architect > *Appear* > Phone: +46 8 545 91 572 > Email: mikael.staldal@appearnetworks.com > --001a11c3783e9c8a1b0503f8b8c9 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Nope. Maybe I'll get around that next week. If you hav= e time to do that, please share!

On Fri, Sep 26, 2014 at 11:23 PM, Mikael St=C3=A5ldal = <mikael.staldal@appearnetworks.com> wrote:
Have you compared the gen= erated byte code (using javap -c) for the two cases?

On Fri, Sep= 26, 2014 at 3:43 PM, Remko Popma <remko.popma@gmail.com> wrote:
On Windows it looks like normal for loops are slig= htly faster than for-each loops, especially for small arrays of primitives.= This could be noise, since we are talking about 5 nanoseconds where the ba= seline (an empty method invocation) is 12 nanos.

On Solaris 10 and Red Hat Enterprise Linux the baseline is so large= (255 nanos and 1910 nanos respectively) that any difference we are seeing = is just noise.

All benchmarks were run wi= th one fork, one thread, 10 warmup iterations and 10 test iterations.

Windows 7 (64bit) with Java 1.8.0_05,= 2-core Intel i5-3317u CPU @1.70Ghz with hyperthreading switched on (4 virt= ual cores)
B= enchmark =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0Mode =C2=A0Samples =C2=A0 =C2=A0 =C2=A0Score =C2= =A0Score error =C2=A0Units
o.a.l.l.p.j.LoopsBenchmark.baseline =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sample =C2=A0 154947 =C2=A0 =C2=A0 12= .432 =C2=A0 =C2=A0 =C2=A0 =C2=A00.550 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray10000ForEa= chLoop =C2=A0 =C2=A0sample =C2=A0 126597 =C2=A0 2759.592 =C2=A0 =C2=A0 =C2= =A0 =C2=A03.431 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray10000ForLoop =C2=A0 =C2=A0 =C2= =A0 =C2=A0sample =C2=A0 126494 =C2=A0 2761.729 =C2=A0 =C2=A0 =C2=A0 =C2=A03= .127 =C2=A0ns/op
o.a= .l.l.p.j.LoopsBenchmark.intArray1000ForEachLoop =C2=A0 =C2=A0 sample =C2=A0= 154124 =C2=A0 =C2=A0292.880 =C2=A0 =C2=A0 =C2=A0 =C2=A01.065 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenc= hmark.intArray1000ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 156155 = =C2=A0 =C2=A0288.751 =C2=A0 =C2=A0 =C2=A0 =C2=A01.101 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.in= tArray100ForEachLoop =C2=A0 =C2=A0 =C2=A0sample =C2=A0 191980 =C2=A0 =C2=A0= 41.826 =C2=A0 =C2=A0 =C2=A0 =C2=A00.870 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray100ForL= oop =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sample =C2=A0 193770 =C2=A0 =C2=A0 36= .894 =C2=A0 =C2=A0 =C2=A0 =C2=A00.782 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray10ForEachL= oop =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 190847 =C2=A0 =C2=A0 22.393 =C2=A0 = =C2=A0 =C2=A0 =C2=A00.618 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray10ForLoop =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 192552 =C2=A0 =C2=A0 17.146 =C2=A0 = =C2=A0 =C2=A0 =C2=A00.560 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray10000ForEachLoop =C2= =A0 =C2=A0sample =C2=A0 173839 =C2=A031959.057 =C2=A0 =C2=A0 =C2=A0 14.341 = =C2=A0ns/op
o.a.l.l.= p.j.LoopsBenchmark.objArray10000ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0sample = =C2=A0 171137 =C2=A032461.985 =C2=A0 =C2=A0 =C2=A0 14.353 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmar= k.objArray1000ForEachLoop =C2=A0 =C2=A0 sample =C2=A0 =C2=A097495 =C2=A0 35= 91.200 =C2=A0 =C2=A0 =C2=A0 =C2=A04.852 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray1000ForL= oop =C2=A0 =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 101560 =C2=A0 3445.998 =C2=A0= =C2=A0 =C2=A0 =C2=A04.010 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray100ForEachLoop =C2=A0= =C2=A0 =C2=A0sample =C2=A0 102796 =C2=A0 =C2=A0438.207 =C2=A0 =C2=A0 =C2= =A0 =C2=A01.923 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray100ForLoop =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0sample =C2=A0 102333 =C2=A0 =C2=A0439.576 =C2=A0 =C2=A0 =C2=A0= =C2=A02.139 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray10ForEachLoop =C2=A0 =C2=A0 =C2=A0 = sample =C2=A0 113924 =C2=A0 =C2=A0 58.957 =C2=A0 =C2=A0 =C2=A0 =C2=A01.247 = =C2=A0ns/op
o.a.l.l.= p.j.LoopsBenchmark.objArray10ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sam= ple =C2=A0 120416 =C2=A0 =C2=A0 60.712 =C2=A0 =C2=A0 =C2=A0 =C2=A01.284 =C2= =A0ns/op

=

// For loops for Object arrays are simila= r but return the total XOR of the element hashcodes.

private int forEachLoop(final int[] array) {
=
=C2=A0 =C2=A0 int result =3D 0;<= /font>
=C2=A0 =C2=A0 for (f= inal int element : array) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 result ^=3D element;
<= font face=3D"courier new, monospace">=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 return result;
<= div>}

private int forLoop(final int[] array) {
=C2=A0 =C2=A0 int result =3D 0;
<= div>=C2=A0 =C2=A0 for (int i =3D 0; i= < array.length; i++) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 result ^=3D array[i];
<= font face=3D"courier new, monospace">=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 return result;
<= div>}



Solaris 10 (64bit) with JDK1.7.0_06-b24 (Oracle Hotspot), 2 qua= d-core Xeon X5570 dual CPUs @2.93Ghz with hyperthreading switched on (16 vi= rtual cores)
Benchmark =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Mode =C2=A0Samples =C2=A0 =C2=A0 =C2=A0Score= =C2=A0Score error =C2=A0Units
o.a.l.l.p.j.LoopsBenchmark.baseline =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sample =C2=A0 110212 =C2=A0 =C2=A0= 255.300 =C2=A0 =C2=A0 =C2=A0 =C2=A00.201 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray10000Fo= rEachLoop =C2=A0 =C2=A0sample =C2=A0 187808 =C2=A0 3938.055 =C2=A0 =C2=A0 = =C2=A0 =C2=A01.207 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray10000ForLoop =C2=A0 =C2=A0 = =C2=A0 =C2=A0sample =C2=A0 187897 =C2=A0 3937.929 =C2=A0 =C2=A0 =C2=A0 =C2= =A00.748 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray1000ForEachLoop =C2=A0 =C2=A0 sample = =C2=A0 123989 =C2=A0 =C2=A0606.631 =C2=A0 =C2=A0 =C2=A0 =C2=A00.626 =C2=A0n= s/op
o.a.l.l.p.j.Loo= psBenchmark.intArray1000ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 1= 23973 =C2=A0 =C2=A0609.565 =C2=A0 =C2=A0 =C2=A0 =C2=A00.416 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchm= ark.intArray100ForEachLoop =C2=A0 =C2=A0 =C2=A0sample =C2=A0 126933 =C2=A0 = =C2=A0294.204 =C2=A0 =C2=A0 =C2=A0 =C2=A00.280 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray1= 00ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sample =C2=A0 127070 =C2=A0 =C2= =A0296.411 =C2=A0 =C2=A0 =C2=A0 =C2=A00.223 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray10Fo= rEachLoop =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 113400 =C2=A0 =C2=A0261.519 = =C2=A0 =C2=A0 =C2=A0 =C2=A00.181 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray10ForLoop =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 111637 =C2=A0 =C2=A0260.435 = =C2=A0 =C2=A0 =C2=A0 =C2=A00.115 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray10000ForEachLoo= p =C2=A0 =C2=A0sample =C2=A0 115872 =C2=A048154.673 =C2=A0 =C2=A0 =C2=A0 18= .846 =C2=A0ns/op
o.a= .l.l.p.j.LoopsBenchmark.objArray10000ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0sam= ple =C2=A0 116777 =C2=A047793.868 =C2=A0 =C2=A0 =C2=A0 17.615 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenc= hmark.objArray1000ForEachLoop =C2=A0 =C2=A0 sample =C2=A0 138432 =C2=A0 525= 6.767 =C2=A0 =C2=A0 =C2=A0 =C2=A02.451 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray1000ForLo= op =C2=A0 =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 136644 =C2=A0 5325.377 =C2=A0 = =C2=A0 =C2=A0 =C2=A02.388 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray100ForEachLoop =C2=A0 = =C2=A0 =C2=A0sample =C2=A0 166653 =C2=A0 =C2=A0773.541 =C2=A0 =C2=A0 =C2=A0= =C2=A00.330 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray100ForLoop =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0sample =C2=A0 166570 =C2=A0 =C2=A0774.513 =C2=A0 =C2=A0 =C2=A0 = =C2=A00.574 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray10ForEachLoop =C2=A0 =C2=A0 =C2=A0 s= ample =C2=A0 178754 =C2=A0 =C2=A0317.232 =C2=A0 =C2=A0 =C2=A0 =C2=A00.134 = =C2=A0ns/op
o.a.l.l.= p.j.LoopsBenchmark.objArray10ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sam= ple =C2=A0 180165 =C2=A0 =C2=A0316.189 =C2=A0 =C2=A0 =C2=A0 =C2=A00.238 =C2= =A0ns/op

=
64 bit RHEL 6.5 (Linux = 2.6.32-431.el6.x86_64) with JDK1.7.0_05-b06 (Oracle Hotspot), 4 quad-core X= eon X5570 CPUs @2.93GHz with hyperthreading switched on (16 virtual cores)<= /b>
Benchmark = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0Mode =C2=A0Samples =C2=A0 =C2=A0 =C2=A0Score =C2=A0Scor= e error =C2=A0Units
= o.a.l.l.p.j.LoopsBenchmark.baseline =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sample =C2=A0 114783 =C2=A0 1910.576 =C2=A0 = =C2=A0 =C2=A0 29.256 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray10000ForEachLoop =C2=A0 =C2= =A0sample =C2=A0 194584 =C2=A0 5132.885 =C2=A0 =C2=A0 =C2=A0 25.137 =C2=A0n= s/op
o.a.l.l.p.j.Loo= psBenchmark.intArray10000ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0sample =C2=A0 1= 96672 =C2=A0 4811.572 =C2=A0 =C2=A0 =C2=A0 52.072 =C2=A0ns/op
<= div>o.a.l.l.p.j.LoopsBenchmark.intArr= ay1000ForEachLoop =C2=A0 =C2=A0 sample =C2=A0 133119 =C2=A0 1967.213 =C2=A0= =C2=A0 =C2=A0 28.970 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray1000ForLoop =C2=A0 =C2=A0 = =C2=A0 =C2=A0 sample =C2=A0 133804 =C2=A0 2004.501 =C2=A0 =C2=A0 =C2=A0 31.= 554 =C2=A0ns/op
o.a.= l.l.p.j.LoopsBenchmark.intArray100ForEachLoop =C2=A0 =C2=A0 =C2=A0sample = =C2=A0 142439 =C2=A0 1575.329 =C2=A0 =C2=A0 =C2=A0 =C2=A06.457 =C2=A0ns/op<= /font>
o.a.l.l.p.j.LoopsBen= chmark.intArray100ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sample =C2=A0 1= 42215 =C2=A0 1957.714 =C2=A0 =C2=A0 =C2=A0 27.815 =C2=A0ns/op
<= div>o.a.l.l.p.j.LoopsBenchmark.intArr= ay10ForEachLoop =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 130826 =C2=A0 1980.301 = =C2=A0 =C2=A0 =C2=A0 30.818 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.intArray10ForLoop =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 132654 =C2=A0 1589.120 =C2=A0 =C2=A0= =C2=A0 =C2=A08.449 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray10000ForEachLoop =C2=A0 =C2= =A0sample =C2=A0 126947 =C2=A043301.320 =C2=A0 =C2=A0 =C2=A0 50.589 =C2=A0n= s/op
o.a.l.l.p.j.Loo= psBenchmark.objArray10000ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0sample =C2=A0 1= 26117 =C2=A043574.129 =C2=A0 =C2=A0 =C2=A0 55.272 =C2=A0ns/op
<= div>o.a.l.l.p.j.LoopsBenchmark.objArr= ay1000ForEachLoop =C2=A0 =C2=A0 sample =C2=A0 143697 =C2=A0 5831.250 =C2=A0= =C2=A0 =C2=A0 19.667 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray1000ForLoop =C2=A0 =C2=A0 = =C2=A0 =C2=A0 sample =C2=A0 163244 =C2=A0 4823.096 =C2=A0 =C2=A0 =C2=A0 13.= 180 =C2=A0ns/op
o.a.= l.l.p.j.LoopsBenchmark.objArray100ForEachLoop =C2=A0 =C2=A0 =C2=A0sample = =C2=A0 162502 =C2=A0 1930.819 =C2=A0 =C2=A0 =C2=A0 24.136 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmar= k.objArray100ForLoop =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sample =C2=A0 171619= =C2=A0 1625.806 =C2=A0 =C2=A0 =C2=A0 10.385 =C2=A0ns/op
<= font face=3D"courier new, monospace">o.a.l.l.p.j.LoopsBenchmark.objArray10F= orEachLoop =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 172226 =C2=A0 1888.683 =C2=A0= =C2=A0 =C2=A0 22.554 =C2=A0ns/op
o.a.l.l.p.j.LoopsBenchmark.objArray10ForLoop =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 sample =C2=A0 188838 =C2=A0 1581.979 =C2=A0 =C2=A0 =C2= =A0 =C2=A06.322 =C2=A0ns/op


=

On Fri, Sep 26, 2014 at 2:56 AM, Matt Sicker <= ;boards@gmail.com= > wrote:
The f= oreach over an array looks like it's supposed to compile to the same th= ing:


<= div>Same goes for .length which is supposed to be a final field which would= allow for inlining by the JIT I'd imagine (hence why we use final ever= ywhere):


On 25 Sep= tember 2014 11:33, Gary Gregory <garydgregory@gmail.com> wrote:
On Thu, Sep 25, 2014 at 11:55 = AM, Ralph Goers <ralph.goers@dslextreme.com> wrote:=
You can think that, but the testing in the testing I did at the time the d= ifference was quite noticeable.=C2=A0 I would have left it as a foreach if = it wasn=E2=80=99t.

Ralph

On Sep= 25, 2014, at 8:51 AM, Paul Benedict <pbenedict@apache.org> wrote:

I would be surprised if foreach over a= n 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.


C= heers,
Paul

On Thu, Sep 25, 2014 at 10:47 AM, Remko Popm= a <remko.popma@gmail.com> wrote:
Hm.. Why did I think it was configuration? I mu= st have gotten mixed up with another commit email...
The class is Marke= rManager in log4j-api.

On Fri, Sep 26, 2014 at 12:18 AM, Ralph Goers <r= alph.goers@dslextreme.com> wrote:
Configuration?=C2=A0 If I r= ecall correctly this method is called on every log event that contains a Ma= rker.=C2=A0 But I am just guessing since Gary neglected to say what class t= his is.=C2=A0 But I do remember doing extensive testing when this code was = written.=C2=A0 And I also remember someone (probably Gary) mentioning then = that it should use a for-loop and we had this same conversation then.=C2=A0= I think that is why the comment was added.

Ralph

On Sep = 24, 2014, at 9:10 PM, Remko Popma <remko.popma@gmail.com> wrote:


On 2014/09/25, at = 12:46, Matt Sicker <boards@gmail.com> wrote:

From what I remember, it had something to do with the incred= ibly large difference in speed between for loops and foreach loops on array= s. 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.)
<= /blockquote>

I'd be curious to see the results!

Gary
=C2=A0
<= div>

On the = other hand, this is configuration, so it only happens once and is very unli= kely to be "hot" code so there is probably not much value in opti= mizing this loop.=C2=A0


On 24 September 2014 22:12,= Gary Gregory <garydgregory@gmail.com> wrote:
Why does this "//noinsp= ection ForLoopReplaceableByForEach" comment mean?

Why not= for an enhanced for each loop?

=C2=A0private static boolean co= ntains(final Marker parent, final Marker... localParents) {
=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 //noinspection ForLo= opReplaceableByForEach
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 for (int i =3D 0, localParentsLength =3D localParents.le= ngth; i < localParentsLength; i++) {
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 final Marker m= arker =3D localParents[i];
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (marker =3D=3D parent) {=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return true;
=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= }
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<= br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 retur= n false;
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }
<= div>
Thanks,
Gary=

=
--



-- Matt Sicker <boar= ds@gmail.com>

=
=

<= /span>




<= font color=3D"#888888">--
Matt Sicker <boards@gmail.com>




<= /div>--
Mikael St=C3=A5ldal=
Chief Software Architect

--001a11c3783e9c8a1b0503f8b8c9--