Skip to content

Instantly share code, notes, and snippets.

@rosenfeld
Created March 8, 2012 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosenfeld/2003503 to your computer and use it in GitHub Desktop.
Save rosenfeld/2003503 to your computer and use it in GitHub Desktop.
Threads performance comparison between JRuby and MRI
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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment