Skip to content

Instantly share code, notes, and snippets.

@wpeterson
Created August 23, 2010 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpeterson/546499 to your computer and use it in GitHub Desktop.
Save wpeterson/546499 to your computer and use it in GitHub Desktop.
# Performance benchmark of native Ruby MD5 digest vs. shelling out
# to openssl md5 utility. Benchmark clearly shows shelling out is
# massively slower, as you might expect.
#
# [master ~/src/cloudfront_asset_host]$> ruby md5_benchmark.rb
# Openssl: 0.110000 0.650000 5.220000 ( 7.547728)
# Native: 0.020000 0.010000 0.030000 ( 0.031182)
require 'benchmark'
require 'digest/md5'
def open_md5(path)
`openssl md5 #{path}`.split(/\s/)[1].to_s
end
def native_md5(path)
Digest::MD5.hexdigest(File.read(path))
end
# Warm it up
10.times { open_md5(__FILE__) }
10.times { native_md5(__FILE__) }
measure = Benchmark.measure { 1000.times { open_md5(__FILE__) } }
puts "Openssl: #{measure}"
measure = Benchmark.measure { 1000.times { native_md5(__FILE__) } }
puts "Native: #{measure}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment