Skip to content

Instantly share code, notes, and snippets.

@marcomd
Created January 6, 2021 21:43
Show Gist options
  • Save marcomd/f516642d174b3efc82274b10a4b1ffd6 to your computer and use it in GitHub Desktop.
Save marcomd/f516642d174b3efc82274b10a4b1ffd6 to your computer and use it in GitHub Desktop.
Benchmark ruby script MD5 vs SHA-256
require 'securerandom'
require 'digest'
require 'benchmark'
# Test weight
n = 1_000
started_time = Time.now
word_1k = SecureRandom.hex(512)
word_10k = SecureRandom.hex(5_120)
word_100k = SecureRandom.hex(51_200)
word_1M = SecureRandom.hex(512_000)
word_10M = SecureRandom.hex(5_120_000)
puts "--- Setup in #{Time.now - started_time} seconds ---"
Benchmark.bm do |x|
x.report('SHA-256 1k ') { n.times { Digest::SHA256.hexdigest word_1k } }
x.report('SHA-256 10k ') { n.times { Digest::SHA256.hexdigest word_10k } }
x.report('SHA-256 100k') { n.times { Digest::SHA256.hexdigest word_100k } }
x.report('SHA-256 1M ') { n.times { Digest::SHA256.hexdigest word_1M } }
x.report('SHA-256 10M ') { n.times { Digest::SHA256.hexdigest word_10M } }
x.report('MD5 1k ') { n.times { Digest::MD5.hexdigest word_1k } }
x.report('MD5 10k ') { n.times { Digest::MD5.hexdigest word_10k } }
x.report('MD5 100k') { n.times { Digest::MD5.hexdigest word_100k } }
x.report('MD5 1M ') { n.times { Digest::MD5.hexdigest word_1M } }
x.report('MD5 10M ') { n.times { Digest::MD5.hexdigest word_10M } }
end
puts "--- Performed in #{Time.now - started_time} seconds ---"
@marcomd
Copy link
Author

marcomd commented Jan 6, 2021

image

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