Ruby 1.9 Quick Speed Test

Sun, Jan 4, 2009 (Programming)

It has been a while since I’ve had a chance to play with Ruby, and with 1.9.1 coming out in January I figured I’d go ahead and install 1.9 to get a feel. 

Since speed was always an issue in 1.8 I figured I’d re-run my fib.rb script to find the fibonacci number for 35.  I ran it against ruby 1.8.7 (2008-08-11 patchlevel 72) and 1.9.0 (2008-06-20 revision 17482), the standard versions for Ubuntu Intrepid Ibex.

Here’s that script:

#!/usr/bin/ruby
require 'time'

puts "starting fibonacci"

def fib(num)
 if(num < 2)
   return 1
 else
   return fib(num-1) + fib(num-2)
 end
end

if ARGV[0] != nil
 if (ARGV[0].to_i >= 35)
  puts "This might take a bit...."
 end
 start = Time.now
 puts fib(ARGV[0].to_i)
 stop = Time.now - start
 puts "Finding the fib of #{ARGV[0]} took:"
 puts stop
 puts "seconds"
end

The test machine is fairly old, which makes it a good candidate to see the speed improvements. It is an old 1.8 GHz P4 with 768MB RAM running KDE4. Here’s what I got:

~/Code/fib$ ruby1.8 fib.rb 35
starting fibonacci
This might take a bit....
14930352
Finding the fib of 35 took:
97.311404
seconds
~/Code/fib$ ruby1.9 fib.rb 35
starting fibonacci
This might take a bit....
14930352
Finding the fib of 35 took:
11.752508888
seconds

Now, it is no news that 1.9 is faster than 1.8, but an 8x improvement on this simple recursive script? I was astonished! This is better than using XRuby to speedup Ruby performance!

Share This on Your Favorite Social Network:
  • Facebook
  • MySpace
  • Twitter
  • Digg
  • StumbleUpon
  • LinkedIn
  • Reddit
  • FriendFeed
  • Tumblr
  • Suggest to Techmeme via Twitter
  • Technorati
  • Mixx
  • Propeller
  • Fark
  • Slashdot
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Print
, , , ,

This post was written by:

Ray Gomez - who has written 46 posts on kallasoft.

Ray, a Linux and Unix nut, spends a majority of his daily ritual programming and testing for Big Blue. In his free time he manages to tweak the currently running thinkpad+KDE4 (WHOA) setup, read, and he occasionally gets out of the fluorescent lights to play roller hockey.

Contact the author

3 Responses to “Ruby 1.9 Quick Speed Test”

  1. Hendra Says:

    Impressive
    I’m going to try Ruby 1.9
    and see if speed improvement it brings is good enough
    for my project
    (I made games with Ruby 1.8 but stopped because the compiler isn’t fast enough)

    Reply

  2. Mathieu Jobin Says:

    ruby 1.8 (shipped with OSX) run it in 18 secs (first macbookpro with penryn’s 6MB L2)

    mathieu-jobins-macbook-pro:~ somekool$ ruby fib.rb 35
    starting fibonacci
    This might take a bit….
    14930352
    Finding the fib of 35 took:
    18.278576
    seconds
    mathieu-jobins-macbook-pro:~ somekool$ ruby –version
    ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
    mathieu-jobins-macbook-pro:~ somekool$

    ——-

    then with ruby 1.9.1 on a old P4 running ArchLinux == 10 seconds

    [somekool@rockinTV ~]$ ruby fib.rb 35
    starting fibonacci
    This might take a bit….
    14930352
    Finding the fib of 35 took:
    10.275866427
    seconds
    [somekool@rockinTV ~]$

    —-

    interesting

    Reply

  3. Wayne Bagguley Says:

    A pure Java version runs in 0.187 seconds. That’s 70 times faster than ruby 1.9 (on my machine)

    Faster = more efficient = better for the environment :D

    Reply

Leave a Reply