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 way.

https://github.com/corneil/compare-fp



On 30 June 2016 at 15:44, Schalk Cronjé <ysb33r@gmail.com> wrote:
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.

Environment

===========

* 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)

 

Options

=======

* Warm Up: Auto (- 60 sec)

* CPU Time Measurement: On

 

            user  system         cpu        real

Each  6271193022   25111  6271218133  6292037678

For   1107577805   20925  1107598730  1108344167

@CompileStatic:

Each  1013972638   22068  1013994706  1033565157

For    249595132    4566   249599698   251259849

 

If I change the code to do something with the result:

@groovy.transform.CompileStatic

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

Jason

 

From: Bob Brown [mailto:bob@transentia.com.au]
Sent: Wednesday, June 29, 2016 6:28 PM
To: users@groovy.apache.org
Subject: Re: For-loop vs each.

 

 

I’ve never benchmarked it,

 

Weirdly enough, I have!

 

I wrote a quick bench benchmark for just this:

 

HTH

 

BOB


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