Skip to content

Instantly share code, notes, and snippets.

@mkroman
Last active October 21, 2024 14:44
Show Gist options
  • Save mkroman/f9b1673721bcdeb8e6daf25129d87190 to your computer and use it in GitHub Desktop.
Save mkroman/f9b1673721bcdeb8e6daf25129d87190 to your computer and use it in GitHub Desktop.
mk@… ~/Projects/Sandbox % cat digest-sha256-file-vs-system-sha256sum.rb
#!/usr/bin/env ruby
require 'tempfile'
require 'digest/sha2'
require 'benchmark/ips'
file = Tempfile.new 'testfile'
file.write 'a' * 1024 * 1024 * 2
Benchmark.ips do |x|
x.report 'Digest::SHA256.file' do
Digest::SHA256.file(file.path).hexdigest
end
x.report '`sha256sum`' do
`sha256sum #{file.path}`.strip
end
end
file.close
file.unlink
# mk@… ~/Projects/Sandbox % ruby digest-sha256-file-vs-system-sha256sum.rb
# Warming up --------------------------------------
# Digest::SHA256.file 72.000 i/100ms
# `sha256sum` 44.000 i/100ms
# Calculating -------------------------------------
# Digest::SHA256.file 738.569 (± 2.0%) i/s - 3.744k in 5.071405s
# `sha256sum` 457.612 (± 7.9%) i/s - 2.288k in 5.034049s
# ruby digest-sha256-file-vs-system-sha256sum.rb 10,21s user 4,17s system 99% cpu 14,393 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment