Skip to content

Instantly share code, notes, and snippets.

@newtonapple
Created October 25, 2009 10:07
Show Gist options
  • Save newtonapple/217986 to your computer and use it in GitHub Desktop.
Save newtonapple/217986 to your computer and use it in GitHub Desktop.
Benchmark: SHA1 vs MD5: SHA1 is about 1.4 x slower
require 'rubygems'
require 'rbench'
require 'digest/md5'
require 'digest/sha1'
MD5 = Digest::MD5
SHA1 = Digest::SHA1
TIMES = 100
short_string = 'this is a short string'
long_string = short_string * 250
RBench.run(TIMES) do
column :sha1
column :md5
column :diff, :title => 'SHA1 / MD5', :compare => [:sha1, :md5]
report 'Short String' do
sha1 { 5000.times { SHA1.hexdigest(short_string) } }
md5 { 5000.times { MD5.hexdigest(short_string) } }
end
report 'Long String' do
sha1 { 5000.times { SHA1.hexdigest(long_string) } }
md5 { 5000.times { MD5.hexdigest(long_string) } }
end
end
# Something to keep in mind
# MD5#hexdigest returns 32 characters
# SHA1#hexdigest returns 40 characters
# SHA1 | MD5 | SHA1 / MD5 |
# -----------------------------------------------------
# Short String 1.638 | 1.626 | 1.01x |
# Long String 10.688 | 7.839 | 1.36x |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment