Skip to content

Instantly share code, notes, and snippets.

@cupakromer
Created December 10, 2012 21:03
Show Gist options
  • Save cupakromer/4253406 to your computer and use it in GitHub Desktop.
Save cupakromer/4253406 to your computer and use it in GitHub Desktop.
Hash Benchmarks
#!/usr/bin/env ruby
require 'benchmark'
require 'digest'
n = 5_000_000
xruns = 10
puts <<-EOS
Hash Benchmarks
===============
Testing MD5, RMD160, SHA1, SHA256, SHA384, SHA512.
WARNING: Both MD5 and SHA1 are known to be insecure.
Running #{n} iterations per hashing method using string concating:
Digest::HashMethod.hexstring(salt + mac)
Performing #{xruns} test runs:
EOS
runs = []
salt = 'eb693ec8252cd630102fd0d0fb7c3485'
mac = '7a0115f60a3c'
xruns.times do |index|
puts "\n\nTest Run #{index + 1}"
runs << Benchmark.bmbm(7){ |x|
x.report("MD5") { 1.upto(n){ Digest::MD5.hexdigest(salt + mac) } }
x.report("RMD160") { 1.upto(n){ Digest::RMD160.hexdigest(salt + mac) } }
x.report("SHA1") { 1.upto(n){ Digest::SHA1.hexdigest(salt + mac) } }
x.report("SHA256") { 1.upto(n){ Digest::SHA256.hexdigest(salt + mac) } }
x.report("SHA384") { 1.upto(n){ Digest::SHA384.hexdigest(salt + mac) } }
x.report("SHA512") { 1.upto(n){ Digest::SHA512.hexdigest(salt + mac) } }
}
end
puts <<-EOS
Results
=======
WARNING: Both MD5 and SHA1 are known to be insecure.
Running #{n} iterations per hashing method using string concating:
Digest::HashMethod.hexstring(salt + mac)
STATS
EOS
%w(MD5 RMD160 SHA1 SHA256 SHA384 SHA512).each_with_index do |hash_method, index|
puts "\n#{hash_method}"
hash_data = runs.map{|r| r[index]}
[:utime, :stime, :total, :real].each do |metric|
data = hash_data.map(&metric)
puts " #{metric.to_s.upcase}"
puts " min max average"
printf(" %9f %9f %9f\n", data.min, data.max, (data.reduce(:+).to_f / data.size))
printf(" %9f %9f %9f\n", data.min/n, data.max/n, (data.reduce(:+).to_f / data.size)/n)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment