Pages

5/02/2013

Ruby Threading Benchmark

I just heard that Ruby 2.0 was improved in threading speed. So I decided to test it out.

My test environment:
  • Intel i-7 CPU (4 physical cores)
  • Ruby versions are run under Rbenv environment
  • Testing versions: 
    • MRI 1.9.3-p0
    • MRI 2.0.0-p0
    • JRuby 1.7.3 (1.9.3p385)
Code:
if ARGV.size > 0
 num_thread = ARGV[0].to_i
else
 num_thread = 1
end

puts "num_threads = #{num_thread}"

MAX_INT = 100000000

threads = []
num_thread.times{|i|
 threads << Thread.new {
  puts "Thread #{i} started"
  work_load = (MAX_INT/num_thread)
  puts "Thread #{i}: work load = #{work_load}"
  a=0
  work_load.times{|i| a=i }
  puts "Thread #{i} stopped"
 }
}

threads.each { |thr| thr.join }

Run Command:
I use "time" command in Linux to measure the time, and bash for-loop to repeating 10 times for each version.
for i in {1..10}; do time ruby testcode.rb; done

Results:

In average:

  • MRI 1.9.3 ==>  14.3178
  • MRI 2.0.0 ==>  13.5061
  • JRuby 1.7.3 ==> 5.9025

Conclusion:

Ruby MRI 2.0.0 is actually improved in thread performance. Unfortunately, it still does not have an actual threading yet. Unlike in JRuby, which it ran each thread in different CPU cores and caused the time to be much lower than the other two.