Skip to content

Instantly share code, notes, and snippets.

@tempredirect
Forked from anonymous/test.rb
Created November 13, 2012 10:04
Show Gist options
  • Save tempredirect/4064984 to your computer and use it in GitHub Desktop.
Save tempredirect/4064984 to your computer and use it in GitHub Desktop.
$ jruby -Xcompile.invokedynamic=true test.rb
Rehearsal -----------------------------------------------------------
with Proc 25.510000 0.280000 25.790000 ( 23.952000)
class Compare 14.490000 0.070000 14.560000 ( 14.395000)
class DelegatingCompare 16.890000 0.080000 16.970000 ( 16.300000)
------------------------------------------------- total: 57.320000sec
user system total real
with Proc 19.120000 0.100000 19.220000 ( 19.305000)
class Compare 14.430000 0.070000 14.500000 ( 14.215000)
class DelegatingCompare 17.780000 0.090000 17.870000 ( 17.463000)
# profile run with the class impl's commented out
$ jruby -Xcompile.invokedynamic=true --profile test.rb
Profiling enabled; ^C shutdown will now dump profile info
Rehearsal ---------------------------------------------
with Proc 19.810000 0.170000 19.980000 ( 17.979000)
----------------------------------- total: 19.980000sec
user system total real
with Proc 17.720000 0.080000 17.800000 ( 17.334000)
main thread profile results:
Total time: 37.77
total self children calls method
----------------------------------------------------------------
35.53 0.00 35.53 2 Benchmark.bmbm
35.51 0.00 35.51 4 Benchmark.measure
35.30 29.30 6.00 2 Object#load_and_clear_map
18.19 0.00 18.19 6 Array#each
18.18 0.00 18.18 1 Enumerable.inject
17.35 0.00 17.34 14 Array#map
6.00 6.00 0.00 24313000 Proc(singleton)#method_missing
1.16 0.10 1.05 100011 Range#each
0.64 0.10 0.54 53 Kernel.require
0.62 0.16 0.47 15 Kernel.load
0.29 0.28 0.01 45 JavaUtilities.get_proxy_or_package_under_package
0.20 0.20 0.00 4 Process.times
0.13 0.00 0.13 8 Java::OrgJruby.method_missing
0.11 0.11 0.00 90009 Kernel.rand
0.08 0.08 0.00 10004 Array#join
0.07 0.00 0.07 5 Kernel.require
0.04 0.04 0.00 5 Time.now
0.03 0.03 0.00 90039 String#<=>
0.03 0.00 0.03 1 Java::OrgJrubyCompilerImpl.method_missing
0.02 0.02 0.00 39 Module#module_eval
0.02 0.00 0.02 2 Java::JavaLang.method_missing
0.02 0.00 0.02 70 Kernel.initialize_dup
0.02 0.02 0.00 69 Module#initialize_copy
0.01 0.00 0.01 10 Object#java_import
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