• Rubinius in the Rough: High Performance Ruby

By James Pitt

Rubinius pulls together bits three of my favorite languages: Lisp, Smalltalk and of course Ruby. Rubinius is really a Ruby implementation from Evan Phoenix, but let’s quickly mention why I brought up Lisp and Smalltalk.

It’s good to take a bit of a step back before we dive into Rubinius. Firstly Lisp – lisp gave us the crazy idea of having a language written in itself (and then hand-compiled). Secondly – Smalltalk. I won’t go into too much details here, but if you are interested take a look at the blue book “Smalltalk-80: The Language and its Implementation” which is available online as well. Smalltalk also had the idea of the language being written in the language itself. Rubinius continues this tradition and uses a lot of the ideas from the bluebook.

Now,  let’s take a look at Rubinius and get an idea about how to actually use it. I’m using Ubuntu, but I know that you can do things in Mac OS without too many problems. If you are using Windows, then can I suggest you just go and download Ubuntu; Rubinius doesn’t run under Windows at the moment. If you have any problems, start to hang out on the Rubinius IRC chat – they are a great set of guys and very knowledgeable.

To use Rubinius, you have to download it and compile it. Let’s start off by doing that:

bash# sudo apt-get install ruby1.8 rubygems git

And get the libraries.

bash# sudo apt-get install ruby-dev libreadline5-dev zlib1g-dev libssl-dev

and llvm (The low level virtual machine, this is for the JIT):

bash# sudo apt-get install llvm llvm-gcc llvm-dev

Now, get the code using git inside your src directory:

bash# git clone git://github.com/evanphx/rubinius.git

Right – now we need to go inside the directory we’ve just ‘gited’ and compile it:

bash# ./configure
bash #rake

Right – with a bit of luck you’ve got things complied. Make sure that you’ve got the gcc g++ installed if you have problems. So – let’s try running Rubinius with the rbx command, and see if it works:

bash# bin/rbx -v
rubinius 1.0.0-rc3 (1.8.7 bfe12b6b 2010-02-16 JI) [i686-pc-linux-gnu]

Now, what happens when we run some simple methods from the command line. To start off with, let’s try this bit of code:

bash# bin/rbx -e 'puts "Hello world"'
Hello world

And to test out the JIT system:

bash# bin/rbx -Xrbx.jit   -e 'puts "Hello JIT!"'
Hello JIT!

Now – if you’re a Rails person, I’m sure you’ll want to get that installed:

bash# bin/rbx gem install rails

Now to start up your rails app you can just do this:

bash# bin/rbx script/server

This seems to work for me, but I’ve heard mixed messages as to if it is totally working yet.

So – as a developer, what is the speed difference between Rubinius and ‘vanilla’ Ruby? I thought it would be good to do a proper comparison between Ubuntu standard Ruby, Ruby complied on the same computer and Rubinius complied on the same computer.

All were run on the same computer, a AMD Athlon(tm) 64 X2 Dual Core Processor 3800+ with 3 Gig ram. All tests were run on a Saturday when the computer wasn’t being used for anything else.  97 tests were ran in total with a range of different benchmarks. Here are the results for the first set of programs, and show the results I got pretty well. All the benchmark code is in the Rubinius distribution.

Total time for code run:
Ruby        Ruby (compiled)    Rubinius
2183.56    2243.92        2174.1

I used apt-get to get the latest version of Ruby, as well as the latest version of the source code which I compiled on my computer. I also installed rubygems and all the usual Ruby code. Everything uses Ruby 1.8.7.

So – what results do we have? The first result is that I’ve not done a good job compiling Ruby on my computer – overall there is little difference, but the compiled version is slightly slower. I think if I was going to do this again, I’d have to tweak the compilation settings to get a bit more speed out of it. However, as I’m writing about Rubinius, let’s talk about that.

Rubinius seems to have come on leaps and bounds over the last few years. It really is pretty competitive now with the standard version of Ruby. Whilst it seems to be slightly behind on some of the very short-run tests, in particular, the maths-based work, on most of the other key tests it’s actually faster. I put together a simple metric to look at the differences for the 3 versions and it came out ahead – overall the code runs faster on Rubinius. In the overall code run time, Rubinius was ahead by a total of 9 seconds . Please not that I’m not a statistician and your mileage will certainly vary a lot. When you look back a few years ago, when Rubinius was running very slowly, it is a much improved system now.

Let’s look at a short program, which used to run very slowly in Rubinius:

require 'benchmark'

def m
nil
end

puts Benchmark.measure {i=0
while i<6000000 # benchmark loop 2
i+=1
m; m; m; m; m; m; m; m;
end}

As you can see, it doesn’t do a lot apart from finding out how quickly the function ‘m’ can be called. Please note that the ‘Benchmark.measure’ is a great little way to find out how fast your functions run. It’s the standard system in Ruby for this. Just ‘require’ it, as I show in the code.

Tuning this in Rubinius gives a total time of 4.924253 seconds. The same program in my version of ‘standard’ Ruby takes 34.855306 seconds. You can really see how this function, which was a good deal slower than the standard version of Ruby is now much faster.

One word of caution about these results. I didn’t use ruby 1.9, which I know is a bit faster than ruby 1.8.7 (the version used in these tests). Ruby 1.9 is a faster than 1.8.7, but I wanted to use the same version of Ruby that Rubinius has implemented. Anyway, Rubinius has come very well on over the last few years, as you can see,  Rubinius is getting close to the speeds of standard Ruby and is certainly an option worth considering going forward.

More Resources

Rubinius http://rubini.us/

About the Author

James Pitt is a developer working mainly in PHP and Ruby. He is based between the UK and the Philippines, where he runs an outsourcing company. He is a specialist in using PHP, Ruby and other open-source technologies to help companies improve their sales.

Share and Enjoy:
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • E-mail this story to a friend!
  • FriendFeed
  • HackerNews
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter
  • FSDaily
  • Ping.fm

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

  1. tmornini Says:

    Thank you for this nice writeup on Rubinius!

    You may want to check out RVM (Ruby Version Manager). It makes installing and using different Ruby implementations very easy.

    gem install rvm

    Once it's configured, it's really easy to pick and choose which implementation you use, and Rubinius support is already in place!

  2. james_pitt Says:

    Ah great – I'm going to give that a try now. It is a bit of a pain switching things over at the moment.

  3. Tweets that mention Rubinius in the Rough: High Performance Ruby -- Topsy.com Says:

    [...] This post was mentioned on Twitter by James Pitt and The Bitsource, Andrew Larkin. Andrew Larkin said: Web Design Issues Rubinius in the Rough: High Performance Ruby: I'm using Ubuntu, but I know that you can do thing… http://bit.ly/aGlnz1 [...]

  4. Tweets that mention Rubinius in the Rough: High Performance Ruby -- Topsy.com Says:

    [...] This post was mentioned on Twitter by The Bitsource, Evgeniy Lobanov. Evgeniy Lobanov said: Rubinius in the Rough: High Performance Ruby By James Pitt http://tinyurl.com/ycchrrr вот это производительность #ruby [...]

  5. Tweets that mention Rubinius in the Rough: High Performance Ruby -- Topsy.com Says:

    [...] This post was mentioned on Twitter by Breaking Tech News. Breaking Tech News said: Rubinius in the Rough: High Performance Ruby http://bit.ly/bctFVT via http://topicfire.com/Technology [...]

Leave a Reply

You must be logged in to post a comment.