Skip to content

Instantly share code, notes, and snippets.

@mikelikesbikes
Last active October 8, 2015 07:07
Show Gist options
  • Save mikelikesbikes/3296193 to your computer and use it in GitHub Desktop.
Save mikelikesbikes/3296193 to your computer and use it in GitHub Desktop.
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