Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created July 14, 2016 07:33
Show Gist options
  • Select an option

  • Save jodosha/866f94ee9c166c5328739770208b1b3f to your computer and use it in GitHub Desktop.

Select an option

Save jodosha/866f94ee9c166c5328739770208b1b3f to your computer and use it in GitHub Desktop.
Compare MD5 digest vs last modified time (mtime) for file tracking
#!/usr/bin/env ruby
require 'benchmark/ips'
require 'digest'
PATH = 'jquery.js'.freeze
FILE = File.new(PATH)
Benchmark.ips do |x|
x.report('mtime') { FILE.mtime.utc.to_i }
x.report('digest') { Digest::MD5.file(PATH).hexdigest }
x.compare!
end
__END__
Results:
Warming up --------------------------------------
mtime 48.278k i/100ms
digest 209.000 i/100ms
Calculating -------------------------------------
mtime 574.006k (± 4.8%) i/s - 2.897M in 5.059809s
digest 2.108k (± 2.5%) i/s - 10.659k in 5.059393s
Comparison:
mtime: 574006.2 i/s
digest: 2108.1 i/s - 272.29x slower
Ruby:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
Hardware:
Hardware Overview:
Model Name: MacBook Pro
Model Identifier: MacBookPro12,1
Processor Name: Intel Core i7
Processor Speed: 3.1 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 4 MB
Memory: 16 GB
Boot ROM Version: MBP121.0167.B16
SMC Version (system): 2.28f7
Software:
System Software Overview:
System Version: OS X 10.11.5 (15F34)
Kernel Version: Darwin 15.5.0
Time since boot: 44 days 1:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment