Skip to content

Instantly share code, notes, and snippets.

@ouranos
Created April 3, 2019 23:48
Show Gist options
  • Save ouranos/17546c1c4256dfdb20220f60175ad57f to your computer and use it in GitHub Desktop.
Save ouranos/17546c1c4256dfdb20220f60175ad57f to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
require 'securerandom'
require 'digest'
NUMBER = 10_000
id_array = Array.new(NUMBER) { SecureRandom.uuid }.freeze
def slow(array)
Digest::MD5.hexdigest(array.join('/'))
end
def fast(array)
array.hash
end
Benchmark.ips do |x|
x.report("MD5#hexdigest") { slow(id_array) }
x.report("Array#hash") { fast(id_array) }
x.compare!
end
Warming up --------------------------------------
MD5#hexdigest 68.000 i/100ms
Array#hash 51.000 i/100ms
Calculating -------------------------------------
MD5#hexdigest 699.184 (± 3.4%) i/s - 3.536k in 5.063895s
Array#hash 520.148 (± 1.9%) i/s - 2.601k in 5.002243s
Comparison:
MD5#hexdigest: 699.2 i/s
Array#hash: 520.1 i/s - 1.34x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment