As I was perusing Proggit yesterday, I ran across a little rant in reply to an earlier blog post on the speed improvements of the new Ruby 1.9 VM vs Python. The Python article was a bit unfair in its assessment of Python's speed vs Ruby's since the author essentially translated a highly recursive algorithm into an iterative one and used that for his comparison. Nevertheless, it did get me a bit more interested in just how much better the new Ruby VM's are performing. After trying the tests in the original article a few times and being truly impressed with Ruby 1.9's nearly 5x speed improvement over the old 1.8 VM, I thought why not give MacRuby a try since it is supposed to be a highly optimized Ruby VM for my computing platform of choice, and let me tell ya, it did not disappoint.

For those of you who are not in the know, MacRuby is a port of Ruby 1.9 for Mac OS X. But where MacRuby differs from other ports of Ruby to the Mac platform, is in its implementation details. MacRuby is built directly on top of Objective-C and, thus, shares many things in common with typical Cocoa apps such as Objective-C's dynamic runtime environment and its garbage collector. What this means for Ruby developers, is a programming language (other than Objective-C) that is essentially a first class citizen in the Mac programming world rather than the typical Objective-C bridge that most other languages provide and which always feel half-baked. The other significant advantage of MacRuby, for those of us lucky enough to be developing for the Mac platform, is its speed. As of MacRuby 0.5 the VM is no longer YARV, but a brand spankin' new VM built upon the LLVM compiler infrastructure which is blazingly fast. Just on a side note, there are so many more goodies that I'd like to address with respect to the MacRuby project, but I'll save those for another post.

Anywho, enough yappin', let's get to the numbers. Below, I've listed the tests that I've run on my machine. For the record, I'm currently running these tests on an Apple MacBook Pro 2.4 GHz Core 2 Duo with 2GB of Memory. Also, the code below is the ruby code that I used for the tests, taken verbatim from Antonio's post, and, yes, before anyone says anything, I do mean to use recursion. This algorithm is purposefully supposed to be CPU intensive so don't bother trying to optimize the code or send me patches. Also, notice the lack of Python in my comparisons here. There is a point to that. I love both languages and I don't want to start any type of flame war here (at least not with this post), I'm merely interested in finding out how the new Ruby VM's are doing. With that said, here's the code:

The Ruby Code


I ran the tests three times for each of the VM's and posted the best speed of the three for each one below. Notice that even MRI's ruby VM has improved by a factor of nearly 5x over 1.8.6. Nice improvements all around, but, of course, in this post we are most interested in the performance improvements found in the new MacRuby VM. I tried out both, the previous beta release and the newest beta and even saw a slight performance bump in the interpreted version of the VM from b1 to b2. Overall, I'm quite impressed with the performance of MacRuby. The interpreted version of the program performed at 10x the speed of the 1.8.6 VM and AOT compilation of the code using macrubyc shaved that time down by yet another 25%. To put this into perspective, we are going from slightly under 50ms to run the code down to just a shade under 2ms for the latest version of the MacRuby VM. Nice!

Ruby 1.8.6
real  0m47.065s
user  0m46.841s
sys   0m0.073s
Ruby 1.9.1
real  0m10.625s
user  0m10.564s
sys   0m0.037s
MacRuby 0.5 beta 1
real  0m1.653s
user  0m1.606s
sys   0m0.037s
MacRuby 0.5 beta 1 (Compiled)
real  0m1.240s
user  0m1.196s
sys   0m0.034s
MacRuby 0.5 beta 2
real  0m1.617s
user  0m1.567s
sys   0m0.042s
MacRuby 0.5 beta 2 (Compiled)
real  0m1.240s
user  0m1.195s
sys   0m0.037s

Finally, just for kicks, I decided to compare the performance of MacRuby against C. You would assume that the C version would smoke Ruby, and it did indeed do much better, a 40% improvement over the compiled MacRuby code, but in the end, both performed admirably. The code below is the C code that I used for the tests. It should be nearly a perfect port of the Ruby code (i.e., neither language should have any unfair advantage over the other). Once you've proven to yourself that the code is kosher, take a look at the performance, I think you'll definitely be impressed by how well MacRuby stacks up. For your convenience, I've gone ahead and listed the MacRuby (compiled) stats again for easier comparison.

The C Code


MacRuby 0.5 beta 2 (Compiled)
real  0m1.240s
user  0m1.195s
sys   0m0.037s
C
real  0m0.737s
user  0m0.727s
sys   0m0.004s

All in all, I'd say the new MacRuby (as well as the new Ruby 1.9 VM) has come along way from the days when any type of CPU intensive activity with Ruby was an absolute nightmare. I've always loved Ruby as a language and now that my one true complaint about it is being addressed by the newest platforms, I find myself constantly moving more and more of my scripting related tasks over to it.

blog comments powered by Disqus