Skip to content

@mikelikesbikes /hamster_v_clojure.rb
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.