-
-
Save colinsurprenant/c556af117e07375313f7 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
source "https://rubygems.org" | |
gem "benchmark-ips" |
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/ips" | |
class Java::JavaUtil::LinkedHashMap | |
# Java 8 Map implements a merge method with a different signature from | |
# the Ruby Hash#merge. see https://github.com/jruby/jruby/issues/1249 | |
if ENV_JAVA['java.specification.version'] >= '1.8' | |
def merge_ruby(other) | |
# commented is a Java implementation, need to bench to see which is most efficient | |
# and if this implementation respects the Ruby merge contract. | |
# | |
dup.merge!(other) | |
end | |
def merge_java(other) | |
duped = Java::JavaUtil::LinkedHashMap.new(self) | |
duped.putAll(other) | |
duped | |
end | |
def merge_java2(other) | |
duped = self.clone | |
duped.putAll(other) | |
duped | |
end | |
def merge_java3(other) | |
result = Java::JavaUtil::LinkedHashMap.new | |
result.putAll(self) | |
result.putAll(other) | |
result | |
end | |
end | |
end | |
m1 = { "hello" => "maximun", "World" => "bye", "nested2" => [1, 2, 3, 4], "array" => [1] } | |
m2 = { "World" => "Uncool", "nested" => { "complex" => "structure" }, "array" => [2,3] } | |
Benchmark.ips do |x| | |
x.warmup = 30 | |
x.time = 30 | |
m = Java::JavaUtil::LinkedHashMap.new(m1) | |
x.report("merge ruby") do | |
m.merge_ruby(m2) | |
end | |
m = Java::JavaUtil::LinkedHashMap.new(m1) | |
x.report("merge java") do | |
m.merge_java(m2) | |
end | |
m = Java::JavaUtil::LinkedHashMap.new(m1) | |
x.report("merge java 2") do | |
m.merge_java2(m2) | |
end | |
m = Java::JavaUtil::LinkedHashMap.new(m1) | |
x.report("merge java 3") do | |
m.merge_java3(m2) | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment