Created
June 6, 2014 09:06
-
-
Save y13i/03ee42352dc66457724f to your computer and use it in GitHub Desktop.
Rubyでのベンチマークのやり方忘れそうなのでメモ
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" | |
N = 10000000 | |
Benchmark.benchmark Benchmark::CAPTION, 20, nil, "total", "average" do |bm| | |
results = {} | |
candidates = { | |
"some_candidate" => ->(i){"do something".capitalize}, | |
"another_candidate" => ->(i){"do something".sub(/\A\w/) {|match| match.upcase}}, | |
} | |
candidates.each do |name, proc| | |
results[name] = bm.report name do | |
N.times &proc | |
end | |
end | |
[ | |
total = results.values.reduce(&:+), | |
total / results.size, | |
] | |
end |
user system total real
some_candidate 3.800000 0.000000 3.800000 ( 3.875910)
another_candidate 7.390000 0.010000 7.400000 ( 7.498499)
total 11.190000 0.010000 11.200000 ( 11.374409)
average 5.595000 0.005000 5.600000 ( 5.687204)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
times何回も書くのアレなのでprocにすると良さげだとおもた