Technology Victims

pátek 23. ledna 2009

Ruby vs. Python - a lame benchmark.

I needed to write some code to merge two english-czech dictionaries. I'm a Rails-guy and Ruby is my language of choice for the time being. That said I now happen to write some of my projects in Python, because I was lured by the Google's App Engine (along with one of my customers). I expected that Python would be faster, so I've decided to write the code in Python for this reason. I also wanted to give an another try to Python scripting and learn some more Python on the way.

I've written that script, and then I've became very curious. What is the actual speed-up? I had no other option, I had to rewrite it in Ruby as well. Because of my laziness I wanted to use the python2ruby converter at first, but it didn't work for me. It took me some time to do the rewrite because of some of the small syntactical differences.

I have to admit, that in Python I like the semantic indentation and list comprehension. Everything else looks better in Ruby for me. I especially hate Python's inconsistent method calling. You write
that is quite normal. But when you want to join, you have to do it like this:
and for checking size, it's yet another way:
Also there was no way to check if array contains an item without using exceptions. I also had to use some ugly hacks to force Python to work correctly with UTF-8 strings. Please correct me if you know better, I'm still a Python noob. Anyway, I've finished the code and I ran it on a Core2Duo MacBook 1.6GHz. I used Python 2.5.2, Ruby 1.8.7 and Ruby 1.9.0. I measured the time it took using the classic time command. The results were following:

Language User time (s) System time (s) CPU Load (%) Total time (s)
Python 7.05 0.37 80 9.204
Ruby 1.9 10.65 0.35 77 14.264
Ruby 1.8.7 21.18 4.93 97 26.776

We can see that Python is still faster than Ruby 1.9 in this benchmark, but not too significantly. Even Ruby 1.8 performs well enough for me to write those scripts in Ruby next time. I'm leaving the interpretation of system time and CPU Load to the more experienced, but i find it somewhat interesting. I could end with this, but I also wanted to give shot to Google's Chart API, so behold:

Ok, we had enough nice graphs, so I'm saying goodbye to the lowhearted, and I'm going to expose the brave ones to the following ugly code:

Python version:

Ruby version:

For the non-enthusiast's among us: Can you spot 10 differences? Well.. that's all for today and I hope that you had some fun reading this post, because while creating it i had plenty...