Ruby 1.9 faster than Ruby 1.8?

Today I ran the following script with Ruby 1.8 & Ruby 1.9 to compare their performances:

  1. def bench  
  2.   start = Time.now  
  3.   1000000.times do  
  4.     yield  
  5.   end   
  6.   puts Time.now - start  
  7. end  
  8.   
  9. puts "Test 1: do things"  
  10. bench {  
  11.   "yeho!12".next  
  12.   rand(100)  
  13.   i ||= 1  
  14.   i = i + 1   
  15. }  
  16.   
  17. puts "Test 2: \"stuff\""  
  18. bench {  
  19.   "stuff"  
  20. }  
  21.   
  22. puts "Test 3: 'stuff'"  
  23. bench {  
  24.   'stuff'  
  25. }  
  26.   
  27. puts "Test 4: :stuff"  
  28. bench {  
  29.   :stuff  
  30. }  

Ruby 1.9 performances are promising:

Test

Ruby 1.8 (sec)

Ruby 1.9 (sec)

Perf Increase
Test 1: do things

1.76

0.54

324.40%
Test 2: "stuff"

0.76

0.21

364.53%
Test 3: 'stuff'

0.80

0.21

388.91%
Test 4: :stuff

0.70

0.13

525.98%

So Ruby 1.9 is 3 to 5 times faster than Ruby 1.8 to run simple operations. I then checked with a small Rails app.

Once I got rubygem installed for Ruby 1.9, the gems I needed installed for Ruby 1.9, the plug-ins I use patched for Ruby 1.9, and my ruby code patched for Ruby 1.9, – yes, it was painful! – I fired: time spec spec

Ruby 1.8

$> time spec spec
............................................

Finished in 0.594813 seconds

44 examples, 0 failures
spec spec  2.49s user 0.79s system 93% cpu 3.522 total

Ruby 1.9

$> time spec spec
............................................

Finished in 0.625589223 seconds

44 examples, 0 failures
spec spec  8.74s user 0.32s system 93% cpu 9.648 total

Grrrr. Ruby 1.8 & 1.9 both pass the specs in ~0.60 second but Ruby 1.9 takes 8.74 seconds in total vs 2.49 seconds for Ruby 1.8. The same behavior occurs when running a Webrick server via script/server: Ruby 1.9 is 2 times slower than Ruby 1.8 to boot up the server and it handles the requests just as fast as Ruby 1.8.

Any Ruby guru to explain such deceiving results?

Post to Twitter

. Bookmark the permalink. Both comments and trackbacks are currently closed.
  • Hi, my name is Philippe Creux.

    This blog is about Agile project management, Ruby programming and other cool things. I haven't published anything here since 2011. Find the latest and greatest on pcreux.com!