Created
January 12, 2017 10:06
-
-
Save Bertg/c51e808f254da9e5068f1fdb1b37c14d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def random_bytes | |
100_000 + rand(128849018880) | |
end | |
require 'benchmark/ips' | |
file_sizes = 350_000.times.map { random_bytes } | |
MEGA_BYTE = 1048576.0 | |
Benchmark.ips do |x| | |
x.time = 5 | |
x.warmup = 2 | |
x.report("devide first") { | |
file_sizes.map{|a| a / MEGA_BYTE }.inject(&:+) | |
} | |
x.report("devide last") { | |
file_sizes.inject(&:+) / MEGA_BYTE | |
} | |
x.compare! | |
end | |
HASH = Hash[500.times.map { [random_bytes, random_bytes] } ] | |
Benchmark.ips do |x| | |
x.time = 5 | |
x.warmup = 2 | |
x.report("inject assign") { | |
HASH.inject({}) do |memo, (k, v)| | |
memo[k] = v | |
memo | |
end | |
} | |
x.report("inject merge") { | |
HASH.inject({}) do |memo, (k, v)| | |
memo.merge({k => v}) | |
end | |
} | |
x.report("inject merge!") { | |
HASH.inject({}) do |memo, (k, v)| | |
memo.merge!({k => v}) | |
end | |
} | |
x.report("with_object assign") { | |
HASH.each_with_object({}) do |(k, v), memo| | |
memo[k] = v | |
end | |
} | |
x.report("with_object merge") { | |
HASH.each_with_object({}) do |(k, v), memo| | |
memo.merge(k => v) | |
end | |
} | |
x.report("with_object merge!") { | |
HASH.each_with_object({}) do |(k, v), memo| | |
memo.merge!(k => v) | |
end | |
} | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment