Quick Ruby vs. Groovy performance comparison

I was just wondering how Ruby and Groovy are comparing in performance with each other so I wrote a quick line in both languages and ran it in irb / GroovyConsole.

Ruby:

start=Time.new;y=0;(1..2000000).each { |x| y=x };Time.new - start

Groovy:

def test=System.currentTimeMillis();(1..2000000).each { x -> y=x };System.currentTimeMillis() - test

The Ruby code is being interpreted, the Groovy one is being compiled into Java Bytecode and then executed.

Ruby: 456 – 529 ms
Groovy: 1812 – 1885 ms

If you change the line inside the closure to “y+=1” you get these results:

Ruby: 674 – 785 ms
Groovy: 4210 – 4465 ms

In Ruby there is no “++” operator, but in Groovy it seems to be faster than “+=1”:

Groovy: 3298 – 3338 ms

Conclusion: it is not very surprising, that a language which is being developed over almost 15 years (Ruby) is better optimised than one which is just 5 years old. I guess if we wait a couple of years Groovy will become a serious competitor to Ruby, but note however that Ruby gets a JIT in version 1.9 so the advance will become much greater.

Advertisements

10 thoughts on “Quick Ruby vs. Groovy performance comparison

  1. Suppose it follows that a 20-year-old language will do better, too. 🙂

    On my machine, median times of three:

    Ruby: assign: 2327ms, increment: 2617ms
    Perl: assign: 264ms, increment: 259ms, increment (++) 214ms

  2. comparing performances of groovy to native ruby is relevant for what use-cases?

    presumably the discriminator that brings groovy into question at all alongside of Ruby is whether to write a JRuby (or perhaps a jython) script vs a groovy option.

    i think the added perl performance quote is spot-on to show how relevant ruby is to either groovy or to perl.

  3. I have had a project where there was a decision against Ruby in favor of Grails and Groovy. The main reason was because Grails runs on a JVM. I guess JRuby would have been the better choice:

    1) start=Time.new;x=1;while x < 2000000 do x+=1 end;Time.new – start
    1160 – 1182 ms

    2) start=Time.new;y=0;(1..2000000).each { |x| y=x };Time.new – start
    795 – 832 ms

  4. My results with 20,000,000 iterations and y+=x:
    Ruby 1.8.6: 31.875
    Ruby 1.9.0: 15.281
    JRuby 1.03: 7.297
    JRuby 1.1RC2: 7.156
    Groovy 1.5.4: 18.344
    Java 1.6.0_5: .016 (not sure what happened here)

    I don’t know how much this sort of test really shows, though. I’ve run other tests where Groovy has come out ahead of Ruby. I’d imagine its better threading support makes up for other differences in the real world, especially if you are considering a Grails vs. Rails project. In my experience, Groovy also uses less ram once the app gets big enough to overshadow the JVM’s bulk. I’m not sure how much the JRuby guys might be changing Ruby’s threads though; do they worry about breaking total compatibility with Ruby 1.8.6 and 1.9?

    Most performance issues only take place in small sections of code, so my vote goes to Groovy and to a lesser extent JRuby. If you’re doing heavy number-crunching, do it in Java. Groovy objects can call and be called by Java objects; I don’t think thats true of JRuby yet?

  5. This test is not very useful because it is not realistic. A realistic test would evaluate the platforms under conditions that they are likely to be used in. The (possible,) advantages of Groovy threading, byte-code optimization, and sophisticated garbage collection provided by the JVM will not be a factor in this simplistic test.

    If you were to put the simplistic benchmark in a function(), and call the function 1000’s of times, you may find that Groovy completely outperforms Ruby, because the JIT compiler of the JVM optimizes the loop away completely (recognizing that it has no side-effects).

    If you are intending on using Ruby or Groovy for single-thread single-shot execution of a loop that has no side-effects then you may very well wish to use Ruby.

    A good article on some of these issues is here:
    http://www.ibm.com/developerworks/java/library/j-jtp12214/index.html?S_TACT=105AGX02&S_CMP=EDU

  6. i have done some projects on php and then I meet with people who need me to built their web based application. I offer using php at the first time and they said … they are not sure if php can do such things but it is okay as long as i have experience building web apps … (they are not so happy). but when i mentioned java .. they looks happy and willing to pay more money …

  7. I copy pasted your groovy code into groovy console, and i am getting 267 ms. I am running groovy 1.7.5

    What version did you run it with?

  8. It is an unequal comparison because you have given the Ruby interpretor a stronger hint as to what you will do with y via the y=0 assignment, prior to iteration. If you add the same hint for Groovy, the performance of the Groovy interpretor improves:


    def test=System.currentTimeMillis();y=0;(1..2000000).each { x -> y=x };System.currentTimeMillis() - test

    Also, Groovy’s main advantage is JIT compilation, but this comparison demonstrates the overhead of JIT compilation, without showing the benefit. To see the benefit, call a method multiple times and compare to Ruby:


    def test() {
    def test=System.currentTimeMillis();def y=0;(1..2000000).each { x -> y=x };println System.currentTimeMillis() - test
    }

    def total=System.currentTimeMillis();(1..10).each {test()};print "Total: "; println System.currentTimeMillis() - total

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s