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

Ruby 2.7.2

Macbook Air M1 16gb gpu 8core

--- Setup in 0.023129 seconds ---
       user     system      total        real
SHA-256 1k    0.001264   0.000332   0.001596 (  0.001593)
SHA-256 10k   0.004959   0.000036   0.004995 (  0.004997)
SHA-256 100k  0.042928   0.000232   0.043160 (  0.043162)
SHA-256 1M    0.415907   0.000826   0.416733 (  0.416718)
SHA-256 10M   4.204848   0.011015   4.215863 (  4.215703)
MD5     1k    0.002682   0.000228   0.002910 (  0.002913)
MD5     10k   0.019295   0.000127   0.019422 (  0.019423)
MD5     100k  0.186654   0.000339   0.186993 (  0.186989)
MD5     1M    1.860531   0.004408   1.864939 (  1.864870)
MD5     10M  18.632030   0.043973  18.676003 ( 18.675280)
--- Performed in 25.455213 seconds ---

Macbook Pro 15" i7 8850H 16gb 6c / 12t

--- Setup in 0.02991 seconds ---
       user     system      total        real
SHA-256 1k    0.004071   0.000653   0.004724 (  0.004872)
SHA-256 10k   0.025718   0.000117   0.025835 (  0.025866)
SHA-256 100k  0.228122   0.000361   0.228483 (  0.229041)
SHA-256 1M    2.272195   0.002773   2.274968 (  2.279683)
SHA-256 10M  22.995221   0.034140  23.029361 ( 23.090202)
MD5     1k    0.003163   0.000437   0.003600 (  0.006347)
MD5     10k   0.014109   0.000038   0.014147 (  0.014159)
MD5     100k  0.128765   0.000218   0.128983 (  0.129492)
MD5     1M    1.274319   0.002110   1.276429 (  1.280381)
MD5     10M  12.797855   0.017020  12.814875 ( 12.844965)
--- Performed in 39.936164 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