Skip to content

Instantly share code, notes, and snippets.

@teeparham
Created November 15, 2013 21:31
Show Gist options
  • Save teeparham/7491991 to your computer and use it in GitHub Desktop.
Save teeparham/7491991 to your computer and use it in GitHub Desktop.
benchmark simple ruby string to int hashing
require 'benchmark'
strs = 1000.times.map{(0...32).map{(65 + rand(26)).chr}.join}
Benchmark.bm(6) do |x|
x.report('sum') { 100.times{strs.each &:sum} }
x.report('digest') { 100.times{strs.each{|s| Digest::MD5.hexdigest(s).to_i(16) }}}
end
# user system total real
# sum 0.000000 0.000000 0.000000 ( 0.007893)
# digest 0.290000 0.030000 0.320000 ( 0.309167)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment