Simple MRI Ruby v JRuby Performance Comparison
Single threaded compute intensive.
Multi threaded compute intensive.
The compute intensive function is simply a O(n2) function that multiplies the inner loop index by the outer loop index in every iteration.
We will use Jruby version 1.6.7 with 1.9.2 support And MRI Ruby 1.9.2 I will test in my Mac Air 13” i5 dual core
I will run the code as is with both JRuby and MRI without passing any special flags or optimization options to any of the two.
Before doing this test I didn’t know which of the two would perform
better although I kind of expected the case of the multithreaded one.
Keep reading.
file test.rb:
def compute_intensive_stuff(times)
(1..times).each do |i|
(1..times).each do |j|
i * j
end
end
end
require 'benchmark'
include Benchmark
def evaluate
bm(1) do |test|
test.report("method:") do
compute_intensive_stuff 10000
end
end
end
evaluateCarlos-MacBook-Air:jruby-vs-ruby cscarioni$ jruby-1.6.7 --1.9 test.rb
user system total real
method: 8.288000 0.000000 8.288000 ( 8.288000)
Carlos-MacBook-Air:jruby-vs-ruby cscarioni$ ruby-1.9.2-p290 test.rb
user system total real
method: 11.030000 0.040000 11.070000 ( 11.103539)
I ran this more than once and the results were similar to these each time.
JRuby seems faster in this very simple setup by more than 20 - 30%
Now with two threads doing the same amount of work:
def compute_intensive_stuff(times)
(1..times).each do |i|
(1..times).each do |j|
i * j
end
end
end
require 'benchmark'
include Benchmark
def evaluate
bm(1) do |test|
test.report("method:") do
compute_intensive_stuff 10000
end
end
end
t1 = Thread.new do
evaluate
end
t2 = Thread.new do
evaluate
end
t1.join
t2.join
Carlos-MacBook-Air:jruby-vs-ruby cscarioni$ ruby-1.9.2-p290 test.rb
user system total real
user system total real
method:method: 22.460000 0.110000 22.570000 ( 22.676014)
22.640000 0.110000 22.750000 ( 22.850209)
Carlos-MacBook-Air:jruby-vs-ruby cscarioni$ jruby-1.6.7 --1.9 test.rb
user system total real
user system total real
method:method: 11.890000 0.000000 11.890000 ( 11.890000)
12.068000 0.000000 12.068000 ( 12.068000)
We can see that the MRI Ruby version doubled the time for this running,
while the JRuby version only increased like 30% the running time.
More importantly a look at the Activity
Monitor shows that the CPU usage is 100% for the MRI Ruby running
process while for the JRuby process the CPU usage shows 200% usage.
Meaning that in the first case, even when it is multithreaded, only 1
thread is executing
at any given time, not taking advantage of the two cores.
The JRuby version in contrast takes full advantage of the dual core
using the full CPU power. This is because JRuby uses the Thread model
provided by the Java Runtime.
There is a way more comprehensive comparison in the following blog:
http://blog.headius.com/2009/04/how-jruby-makes-ruby-fast.html. It has
great explanations of how to tweak JRuby for performance to make it
faster.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





