public
Created

Threads performance comparison between JRuby and MRI

  • Download Gist
example.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
require 'nokogiri'
require 'benchmark'
require 'thread'
require 'open-uri'
 
# for JRUBY: export JRUBY_OPTS="--1.9 -J-Xmx2048m"
# JRuby 1.6.7 doesn't support File.write yet in 1.9 mode. Just doing some cache here - not important
File.open('test.html', 'w'){|f| f.write(open('http://www.sec.gov/Archives/edgar/data/1129623/000095012310064472/d73737ddefm14a.htm').read) } unless File.exists?('test.html')
content = File.read('test.html')
 
class Executor
def initialize(pool_size)
@available_threads = Queue.new
pool_size.times { @available_threads << nil }
@threads = []
end
 
def execute
@available_threads.pop
@threads << Thread.start {
yield
@available_threads << nil
}
end
 
def shutdown
@threads.each &:join
end
end
 
Benchmark.bmbm do |x|
x.report('Parse HTML') do
executor = Executor.new(10)
50.times { executor.execute { Nokogiri.HTML(content) } }
executor.shutdown
end
end
 
# Results for JRuby 1.6.7:
# Rehearsal ----------------------------------------------
# Parse HTML 4.501000 0.000000 4.501000 ( 4.501000)
# ------------------------------------- total: 4.501000sec
#
# user system total real
# Parse HTML 0.927000 0.000000 0.927000 ( 0.928000)
 
# Results for MRI 1.9.3-p
# Rehearsal ----------------------------------------------
# Parse HTML 3.800000 0.050000 3.850000 ( 3.853381)
# ------------------------------------- total: 3.850000sec
#
# user system total real
# Parse HTML 3.780000 0.010000 3.790000 ( 3.788918)
 
# They perform about the same if I change to Executor.new(1), ie, using a single thread
 
# Am I doing any mistake in this code?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.