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!


3. February 2009 at 12:20 am
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)
6. November 2009 at 4:39 pm
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
31. January 2010 at 5:12 pm
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