Holy Shmoly, Ruby 1.9 smokes Python away!

Alright the title of this post is a tad sensational sounding, I know, and it’s in part aimed at messing with my many Pythonista friends. We Rubyists have been teased for a long time, due to the slowness of the main Ruby interpreter. Well, it looks like with Ruby 1.9, it’ll be payback time. Just out of curiosity I decided to run a single benchmark (you can hardly call it that) to see how Ruby 1.9 had improved over the current stable version (1.8.6). I wasn’t planning to make a post about it. It was one of those tests that you do at 3 AM in an irb session when you feel you’ve made your daily peace with your actual workload for the night. When I saw the results though, my jaw dropped. I had to blog about this one.

I ran a recursive Fibonacci function, just to stress test a bit of recursion and method calling, and while I was at it, I decided to compare it with Python too. The test was run on Mac OS X 10.5 with my MacBook Pro (Core 2 Duo 2.2 GHz and 2 GB of memory). It’s a single test (which is obviously not a real world example, as you would use an iterative version of the function if it were), and unlike with real programs, it doesn’t stress many features of the language. At least for now, there is no reasonable evidence to conclude that Ruby 1.9 – which will be released for this coming Christmas – will actually be faster than Python 2.5.1 in the majority of situations, but hear me out and check out these very surprising results.

The Ruby code:

def fib(n)
  if n == 0 || n == 1
    n
  else
    fib(n-1) + fib(n-2)
  end
end

36.times do |i| 
  puts "n=#{i} => #{fib(i)}"
end

And the Python equivalent:

def fib(n):
   if n == 0 or n == 1:
      return n
   else:
      return fib(n-1) + fib(n-2)

for i in range(36):
    print "n=%d => %d" % (i, fib(i))

Running the snippets above, I got the following results:

Ruby 1.8.6:       158.869s
Python 2.5.1:      31.507s
Ruby 1.9.0:        11.934s

Ehm, hold on a second! Did Ruby just go from 159 seconds down to 12? Koichi Sasada, do you have an Amazon Wishlist? I was expecting a decent improvement, as I’ve been playing with 1.9 every now and then for a long time – so I knew it was faster – but I was blown away when I timed the latest version from trunk (even if it’s a really silly example that’s being tested). Granted Python is not the fastest language out there, but Ruby 1.9 was still able to execute the script almost 3 times as fast. It’s unbelievable.

Now it’ll be very interesting to run a series of algorithmically equivalent tests for Ruby and Python, and to see just when exactly Ruby 1.9 manages to knock Python out of the water – and where Python has still the edge. If I manage to find some time, I will report the results in this blog. But for now, I’ll say just… wow!

Get more stuff like this

Subscribe to my mailing list to receive similar updates about programming.

Thank you for subscribing. Please check your email to confirm your subscription.

Something went wrong.

104 Comments

  1. Bruno November 28, 2007
  2. karim November 28, 2007
  3. vuk November 28, 2007
  4. Christian November 28, 2007
  5. xtian November 28, 2007
  6. gnufied November 28, 2007
  7. Oliver November 28, 2007
  8. Pingback: Ruby1.9 vs Python 2.5.1 « Andrei Maxim November 28, 2007
  9. Greg Graham November 28, 2007
  10. Andrew Ingram November 28, 2007
  11. she November 28, 2007
  12. Antonio Cangiano November 28, 2007
  13. Chris Khoo November 28, 2007
  14. Leonardo November 28, 2007
  15. foo November 28, 2007
  16. arman November 28, 2007
  17. Pingback: Twisted Electricity November 28, 2007
  18. dyork November 28, 2007
  19. Del Ben Oscar November 28, 2007
  20. Ola Bini November 28, 2007
  21. Dima Dogadaylo November 28, 2007
  22. pp November 28, 2007
  23. Thomas Güttler November 28, 2007
  24. Anurag November 28, 2007
  25. Bob Holness November 28, 2007
  26. Jaroslaw Zabiello November 28, 2007
  27. tim November 28, 2007
  28. Jan Rychter November 28, 2007
  29. mkaatman November 28, 2007
  30. Martin Vilcans November 28, 2007
  31. sapphirecat November 28, 2007
  32. Larry McVoy November 28, 2007
  33. Dima Dogadaylo November 28, 2007
  34. dataangel November 28, 2007
  35. Sobe November 28, 2007
  36. Andrew November 28, 2007
  37. toreau November 28, 2007
  38. Logan November 28, 2007
  39. mfp November 28, 2007
  40. pp November 28, 2007
  41. Isaac Gouy November 28, 2007
  42. Matthew November 28, 2007
  43. raggi November 28, 2007
  44. bascule November 28, 2007
  45. Dan November 28, 2007
  46. Charles Oliver Nutter November 28, 2007
  47. Pingback: Ruby 1.9 « .dscape November 28, 2007
  48. Pingback: » The Links » roarin’ reporter November 28, 2007
  49. Paolo Bonzini November 29, 2007
  50. Pingback: Paw Prints » Blog Archive » Parallelism November 29, 2007
  51. Jaroslaw Zabiello November 29, 2007
  52. ARJ November 29, 2007
  53. Pingback: Links on a more serene thursday November 29, 2007
  54. Antonio Cangiano November 29, 2007
  55. SwitchBL8 November 29, 2007
  56. Antonio Cangiano November 29, 2007
  57. ictboy November 30, 2007
  58. vicalloy November 30, 2007
  59. niko December 1, 2007
  60. Pingback: Dinomite.net » Perl, Python and Ruby December 1, 2007
  61. Pingback: clorophilla.blog December 1, 2007
  62. SwitchBL8 December 2, 2007
  63. luke December 3, 2007
  64. Antonio Cangiano December 3, 2007
  65. Pingback: ??????? » Blog Archive » Ruby 1.9 December 3, 2007
  66. lvcha December 5, 2007
  67. Pingback: I wanna spend all your money... December 5, 2007
  68. Adam December 5, 2007
  69. Pingback: Ruby o Phyton? December 7, 2007
  70. aaa December 10, 2007
  71. Sturla Molden December 11, 2007
  72. Antonio Cangiano December 11, 2007
  73. Sturla Molden December 11, 2007
  74. ShiningRay December 17, 2007
  75. BLeAm December 25, 2007
  76. HYPER January 4, 2008
  77. adit January 20, 2008
  78. Satish February 8, 2008
  79. JMC February 28, 2008
  80. Diego Viola April 6, 2008
  81. Hodak April 8, 2008
  82. bogdan May 5, 2008
  83. bogdan May 5, 2008
  84. Jorge August 16, 2008
  85. Linan September 7, 2008
  86. Twey November 10, 2008
  87. peke November 28, 2008
  88. attrappe December 22, 2008
  89. Roger December 24, 2008
  90. Tomáš Holas January 23, 2009
  91. Justin January 30, 2009
  92. obnoxiousguy February 2, 2009
  93. Salem February 5, 2009
  94. Antonio Cangiano February 5, 2009
  95. Salem February 5, 2009
  96. Salem February 5, 2009
  97. Salem February 5, 2009
  98. hoppinjohns February 19, 2009
  99. glen worstell March 25, 2009
  100. Donnie March 28, 2009
  101. Pingback: Dinomite.net » Perl, Python and Ruby April 17, 2009
  102. Vadim Fint May 14, 2009
  103. Vadim Fint May 14, 2009
  104. Mark May 16, 2009
  105. Eric May 28, 2009
  106. Diego Viola June 1, 2009
  107. Real World June 21, 2009
  108. Jorrit Posthuma July 11, 2009
  109. Jorrit Posthuma July 11, 2009
  110. roger November 20, 2009
  111. peddro vallle December 20, 2011
  112. michael December 28, 2011
  113. Rohan Jain April 12, 2012
    • Johan Nestaas November 28, 2012

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.