Skip to content

Instantly share code, notes, and snippets.

@pmahoney
Created September 13, 2012 15:50
Show Gist options
  • Save pmahoney/3715259 to your computer and use it in GitHub Desktop.
Save pmahoney/3715259 to your computer and use it in GitHub Desktop.
Crummy kind_of? or respond_to? benchmark under JRuby
require 'java'
require 'benchmark'
s = Java::JavaLang::String.new('hello')
n = 10000000
respond_task = proc { n.times { s.respond_to?(:to_s) || abort('error') } }
no_respond_task = proc { n.times { s.respond_to?(:no_method) && abort('error') } }
kind_of_task = proc { n.times { s.kind_of?(Java::JavaLang::String) || abort('error') } }
no_kind_of_task = proc { n.times { s.kind_of?(Java::JavaLang::Integer) && abort('error') } }
tasks = {
:respond => respond_task,
:no_respond => no_respond_task,
:kind_of => kind_of_task,
:no_kind_of => no_kind_of_task
}
Benchmark.bm do |bm|
puts "Warming up JRuby and JVM"
# warmup jruby / jvm
tasks.each do |name, task|
bm.report("cold %10s:" % name, &task)
end
puts
puts "Real test"
tasks.each do |name, task|
bm.report("warm %10s:" % name, &task)
end
end
$ ruby bench_kind_of_require.rb
user system total real
Warming up JRuby and JVM
cold respond: 0.577000 0.000000 0.577000 ( 0.547000)
cold no_respond: 0.475000 0.000000 0.475000 ( 0.475000)
cold kind_of: 0.639000 0.000000 0.639000 ( 0.639000)
cold no_kind_of: 0.852000 0.000000 0.852000 ( 0.852000)
Real test
warm respond: 0.456000 0.000000 0.456000 ( 0.456000)
warm no_respond: 0.505000 0.000000 0.505000 ( 0.505000)
warm kind_of: 0.564000 0.000000 0.564000 ( 0.564000)
warm no_kind_of: 0.819000 0.000000 0.819000 ( 0.818000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment