Created
April 3, 2019 23:48
-
-
Save ouranos/17546c1c4256dfdb20220f60175ad57f 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
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 |
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
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