groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Winnebeck, Jason" <>
Subject RE: For-loop vs each.
Date Thu, 30 Jun 2016 14:35:53 GMT
Thanks for that update. That is not good news for me, I am very excited about lambdas and functional
programming in Java 8+, but less excited when seeing how much of a performance difference
there is, 30x slower in Java. Hopefully the situation improves in Java 9.


From: []
Sent: Thursday, June 30, 2016 10:04 AM
Subject: Re: For-loop vs each.

I did an exercise a while ago comparing imperative and functional implementations of FizzBuzz
in various JVM languages.
It was then obvious that an imperative loop will still outperform a closure/lamba by a long

Corneil du Plessis

On 30 June 2016 at 15:44, Schalk Cronjé <<>>
Definitely some good information here. At least it answered the basic question I had.

On 30/06/2016 14:11, Winnebeck, Jason wrote:
That’s cool. I see you did it on Java 7. I wondered if for such a trivial example if things
change in Java 8 or 9. I’ve heard that JVM can do smart things with stack allocation to
eliminate GC and inlining. On your system you saw almost 4x increase. On mine I see almost
6x. However, I don’t know how well gbench does like JMH in terms of dead-code analysis –
and this loop would definitely count as dead code. I also tried an example to eliminate the
dead code.
* Groovy: 2.4.6
* JVM: Java HotSpot(TM) 64-Bit Server VM (25.74-b02, Oracle Corporation)
    * JRE: 1.8.0_74
    * Total Memory: 214.5 MB
    * Maximum Memory: 1794 MB
* OS: Windows 7 (6.1, amd64)

* Warm Up: Auto (- 60 sec)
* CPU Time Measurement: On

            user  system         cpu        real
Each  6271193022   25111  6271218133  6292037678<tel:6292037678>
For   1107577805   20925  1107598730  1108344167
Each  1013972638   22068  1013994706  1033565157
For    249595132    4566   249599698   251259849

If I change the code to do something with the result:
def doEach() {{ it->
        int i = 0
        (1..100000000).each { int x -> i *= x }
        println i

Now the results are this:

            user  system         cpu        real
Each  7768820885   19783  7768840668  7832590835
For    834584044   11414   834595458   859830590

From: Bob Brown []
Sent: Wednesday, June 29, 2016 6:28 PM
Subject: Re: For-loop vs each.

I’ve never benchmarked it,

Weirdly enough, I have!

I wrote a quick bench benchmark for just this:


This email message and any attachments are for the sole use of the intended recipient(s).
Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply email and destroy all copies of the
original message and any attachments.


Schalk W. Cronjé

Twitter / Ello / Toeter : @ysb33r

View raw message