Agile Developer, Berlin, Germany

24.01.2008

Quick Ruby vs. Groovy performance comparison

Filed under: programming — pegolon @ 12:24
Tags: ,

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.

7 Comments »

  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

    Comment by Andrew — 28.01.2008 @ 22:58 | Reply

  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.

    Comment by jimn — 12.02.2008 @ 7:29 | Reply

  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

    Comment by pegolon — 12.02.2008 @ 10:12 | Reply

  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?

    Comment by Grant — 14.03.2008 @ 14:18 | Reply

  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

    Comment by Shah Bulkin — 15.07.2008 @ 9:23 | Reply

  6. I vote for Groovy :) it has nice language syntax than ruby.

    I still love C/Java style ;)

    Comment by adwin — 13.09.2008 @ 7:57 | Reply

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

    Comment by jcadawell — 15.12.2008 @ 6:31 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.