Skip to content

Instantly share code, notes, and snippets.

@am-kantox
Created March 29, 2017 14:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save am-kantox/f5afd59fda82c2a3ffe0a4c3e08a1548 to your computer and use it in GitHub Desktop.
Save am-kantox/f5afd59fda82c2a3ffe0a4c3e08a1548 to your computer and use it in GitHub Desktop.
Ruby :: benchmark of different digests
require 'benchmark/ips'
n = 500000
require 'digest/md5'
require 'digest/sha1'
require 'digest/sha2'
require 'digest/sha3' # https://github.com/phusion/digest-sha3-ruby
INPUT = [5, "Hello, world", nil]
Benchmark.ips do |x|
x.config(:time => 5, :warmup => 2)
x.report('md5') { Digest::MD5.hexdigest(INPUT.inspect) }
x.report('sha1') { Digest::SHA1.hexdigest(INPUT.inspect) }
x.report('sha2') { Digest::SHA2.hexdigest(INPUT.inspect) }
x.report('sha3/224') { Digest::SHA3.hexdigest(INPUT.inspect, 224) }
x.report('sha3/512') { Digest::SHA3.hexdigest(INPUT.inspect) }
x.compare!
end
# Warming up --------------------------------------
# md5 47.385k i/100ms
# sha1 47.681k i/100ms
# sha2 35.042k i/100ms
# sha3/224 26.992k i/100ms
# sha3/512 27.186k i/100ms
# Calculating -------------------------------------
# md5 540.017k (± 2.4%) i/s - 2.701M in 5.004540s
# sha1 521.016k (± 2.3%) i/s - 2.622M in 5.035980s
# sha2 372.515k (± 3.8%) i/s - 1.892M in 5.087371s
# sha3/224 293.976k (± 2.2%) i/s - 1.485M in 5.052466s
# sha3/512 289.137k (± 2.0%) i/s - 1.468M in 5.079475s
#
# Comparison:
# md5: 540017.0 i/s
# sha1: 521015.8 i/s - same-ish: difference falls within error
# sha2: 372515.3 i/s - 1.45x slower
# sha3/224: 293976.3 i/s - 1.84x slower
# sha3/512: 289136.6 i/s - 1.87x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment