DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Simple MRI Ruby v JRuby Performance Comparison

Carlo Scarioni user avatar by
Carlo Scarioni
·
Jun. 28, 12 · Interview
Like (0)
Save
Tweet
Share
9.63K Views

Join the DZone community and get the full member experience.

Join For Free
The following is a very simplistic performance comparison running 2 program examples, running them with both MRI Ruby and JRuby, in the following categories:

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

evaluate

Carlos-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. 

 

Jruby Comparison (grammar)

Published at DZone with permission of Carlo Scarioni, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Enterprise Portal and How to Develop One?
  • 8 Proven Ways to Combat End-of-Life Software Risks
  • MongoDB Time Series Benchmark and Review
  • Unlocking the Power of ChatGPT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: