Skip to content

Instantly share code, notes, and snippets.

@evanmiller67
Created September 8, 2014 18:57
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 evanmiller67/4658b1b05c805b4c8ed2 to your computer and use it in GitHub Desktop.
Save evanmiller67/4658b1b05c805b4c8ed2 to your computer and use it in GitHub Desktop.
i18n hash benchmark and results
#!/usr/bin/env ruby
require 'benchmark'
require 'digest'
require 'xxhash'
md5 = Digest::MD5.new
sha1 = Digest::SHA1.new
rmd160 = Digest::RMD160.new
sha256 = Digest::SHA256.new
sha384 = Digest::SHA384.new
sha512 = Digest::SHA512.new
c_md5 = Digest::MD5
c_sha1 = Digest::SHA1
c_rmd160 = Digest::RMD160
c_sha256 = Digest::SHA256
c_sha384 = Digest::SHA384
c_sha512 = Digest::SHA512
seed = 16_777_213
key = 'a' * 100
Benchmark.bm(15) do |b|
b.report("Object#hash") do
10_000.times do
"i18n/en/#{key.hash}"
end
end
b.report("xxhash") do
10_000.times do
"i18n/en/#{XXhash.xxh32(key,16777213)}"
end
end
b.report("md5") do
10_000.times do
"i18n/en/#{md5.hexdigest(key)}"
end
end
b.report("sha1") do
10_000.times do
"i18n/en/#{sha1.hexdigest(key)}"
end
end
b.report("rmd160") do
10_000.times do
"i18n/en/#{rmd160.hexdigest(key)}"
end
end
b.report("sha256") do
10_000.times do
"i18n/en/#{sha256.hexdigest(key)}"
end
end
b.report("sha384") do
10_000.times do
"i18n/en/#{sha384.hexdigest(key)}"
end
end
b.report("sha512") do
10_000.times do
"i18n/en/#{sha512.hexdigest(key)}"
end
end
b.report("md5 - class") { 10_000.times { "i18n/en/#{c_md5.hexdigest(key)}" } }
b.report("sha1 - class") { 10_000.times { "i18n/en/#{c_sha1.hexdigest(key)}" } }
b.report("rmd160 - class") { 10_000.times { "i18n/en/#{c_rmd160.hexdigest(key)}" } }
b.report("sha256 - class") { 10_000.times { "i18n/en/#{c_sha256.hexdigest(key)}" } }
b.report("sha384 - class") { 10_000.times { "i18n/en/#{c_sha384.hexdigest(key)}" } }
b.report("sha512 - class") { 10_000.times { "i18n/en/#{c_sha512.hexdigest(key)}" } }
end
$ ./benchmark.rb
user system total real
Object#hash 0.010000 0.000000 0.010000 ( 0.010129)
xxhash 0.000000 0.000000 0.000000 ( 0.006071)
md5 0.010000 0.010000 0.020000 ( 0.011946)
sha1 0.020000 0.000000 0.020000 ( 0.014382)
rmd160 0.020000 0.000000 0.020000 ( 0.021107)
sha256 0.020000 0.000000 0.020000 ( 0.019214)
sha384 0.020000 0.000000 0.020000 ( 0.018470)
sha512 0.020000 0.000000 0.020000 ( 0.024694)
md5 - class 0.020000 0.000000 0.020000 ( 0.025295)
sha1 - class 0.020000 0.000000 0.020000 ( 0.019504)
rmd160 - class 0.030000 0.000000 0.030000 ( 0.022158)
sha256 - class 0.020000 0.000000 0.020000 ( 0.025232)
sha384 - class 0.030000 0.000000 0.030000 ( 0.026837)
sha512 - class 0.030000 0.000000 0.030000 ( 0.028532)
@evanmiller67
Copy link
Author

The 100 character key at L22 was chosen because that's the longest key length currently used privately. I figure that's a reasonable benchmark length as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment