Created
August 23, 2010 22:45
-
-
Save wpeterson/546499 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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