public
Last active

  • Download Gist
hamster_v_clojure.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
require 'benchmark'
require 'hamster'
require 'deps/clojure-1.4.0.jar'
 
Benchmark.bm do |bm|
def fill(hash)
(0..10000).reduce(hash) do |h, i|
if h.class == Java::ClojureLang::PersistentHashMap
h.assoc(i, 1)
elsif h.respond_to?(:put)
h.put(i, 1)
else
h[i] = 1
h
end
end
end
 
ruby_hash = fill(Hash.new)
hamster_hash = fill(Hamster.hash())
clojure_hash = fill(Java::ClojureLang::PersistentHashMap::EMPTY)
 
bm.report(" ::Hash with native select") {
ruby_hash.select { |k, _| k.odd? }
}
 
bm.report(" Hamster::Hash with native select") {
hamster_hash.select { |k, _| k.odd? }
}
 
bm.report(" Java::ClojureLang::PersistentHashMap with native select") {
clojure_hash.select { |k, _| k.odd? }
}
 
bm.report(" ::Hash with reduce-based select") {
ruby_hash.reduce(Hash.new) { |h, (k, v)| h[k] = v if k.odd?; h }
}
 
bm.report(" Hamster::Hash with reduce-based select") {
hamster_hash.reduce(Hash.new) { |h, (k, v)| h[k] = v if k.odd?; h }
}
 
bm.report("Java::ClojureLang::PersistentHashMap with reduce-based select") {
clojure_hash.reduce(Hash.new) { |h, (k, v)| h[k] = v if k.odd?; h }
}
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.