Skip to content

Instantly share code, notes, and snippets.

@j-wilkins
Created December 27, 2011 19:45
Show Gist options
  • Save j-wilkins/1524935 to your computer and use it in GitHub Desktop.
Save j-wilkins/1524935 to your computer and use it in GitHub Desktop.
SHA1 point-making code.
A stupid example that shows nothing aside from the fact that SHA1 is fast. it's probably documented much more
thoroughly elsewhere.
The gist is this: we take approximately 100000 file paths, see how long it takes to loop through them purely for
comparison, then we see how long it takes to hash all 100000, then we go through and compare the string file paths
to an arbitrary string file path, and finally we go through and compare the hashed file path to a hashed arbitrary
file path.
The general results showing that, while the SHA1 does take significantly more time than a straight loop, it is still
fairly minuscule < .5 seconds. while you wouldn't want to be SHA1'ing 100000 file paths all the time, if you had to,
it's not really all that bad.
RESULTS:
100002 sample file paths
user system total real
Simple Loop 0.010000 0.000000 0.010000 ( 0.014255)
Hash Time 0.400000 0.010000 0.410000 ( 0.405802)
Equality of File path comparison 0.020000 0.000000 0.020000 ( 0.021672)
Equality of Hashed path comparison 0.420000 0.000000 0.420000 ( 0.422237)
So, if my math is correct (unlikely) it takes about 0.0000042 seconds per SHA1.
require 'benchmark'
require 'digest/sha1'
count = 0
$files = Array.new
$hashes = Array.new
Dir["#{ENV['HOME']}/workspace/**/*"].each do |f|
$files << f
break if count > 100000
count += 1
end
puts "#{$files.length} sample file paths"
comp_string = "/No/Such/Filepath/sorry/you/are/out/of_luck/here.pdf"
Benchmark.bm(35) do |bm|
bm.report("Simple Loop") { $files.each {}}
bm.report("Hash Time") { $files.each {|f| $hashes << Digest::SHA1.hexdigest(f)}}
bm.report("Equality of File path comparison") { $files.each {|f| f == comp_string}}
bm.report("Equality of Hashed path comparison") { $hashes.each {|h| h == Digest::SHA1.hexdigest(comp_string) }}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment