Created
November 13, 2012 10:03
-
-
Save anonymous/4064982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'Benchmark' | |
require 'java' | |
require 'jruby/profiler' | |
java_import java.util.TreeMap | |
# data to mess with | |
values = (0..10000).map { (0..8).map{ ('a'..'z').to_a[rand(26)] }.join } | |
cmp = Proc.new {|a,b|a <=> b } | |
class Compare | |
java_implements java.util.Comparator | |
java_signature 'int compare(java.lang.Object,java.lang.Object)' | |
def compare(a, b) | |
a <=> b | |
end | |
end | |
class DelegatingCompare | |
java_implements java.util.Comparator | |
def initialize &block | |
@proc = block.to_proc | |
end | |
java_signature 'int compare(java.lang.Object,java.lang.Object)' | |
def compare(a, b) | |
@proc.call(a, b) | |
end | |
end | |
Benchmark.bmbm do |x| | |
def load_and_clear_map(map,values) | |
100.times do | |
values.each { |v| map.put(v,v) } | |
map.clear() | |
end | |
end | |
x.report("with Proc") do | |
map = TreeMap.java_class.constructor(java::util::Comparator).new_instance(cmp).to_java | |
load_and_clear_map(map,values) | |
end | |
x.report("class Compare") do | |
map = TreeMap.java_class.constructor(java::util::Comparator).new_instance(Compare.new).to_java | |
load_and_clear_map(map,values) | |
end | |
x.report("class DelegatingCompare") do | |
map = TreeMap.java_class.constructor(java::util::Comparator).new_instance(DelegatingCompare.new {|a,b| a <=> b}).to_java | |
load_and_clear_map(map,values) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment