Skip to content

Instantly share code, notes, and snippets.

Created April 6, 2012 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2318872 to your computer and use it in GitHub Desktop.
Save anonymous/2318872 to your computer and use it in GitHub Desktop.
require 'benchmark'
times = 500000
def gen_string
rand(1<<100).to_s(36)
end
Benchmark.bm(100) do |x|
x.report('hashes') {
stringhash = {}
strings = []
times.times do
strings << gen_string
stringhash[strings.last.hash] ||= 0
stringhash[strings.last.hash] += 1
end
strings.each do |string|
stringhash[string.hash] += 1
end
}
x.report('strings') {
stringhash = {}
strings = []
times.times do
strings << gen_string
stringhash[strings.last] ||= 0
stringhash[strings.last] += 1
end
strings.each do |string|
stringhash[string] += 1
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment