Skip to content

Instantly share code, notes, and snippets.

@y13i
Created June 6, 2014 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y13i/03ee42352dc66457724f to your computer and use it in GitHub Desktop.
Save y13i/03ee42352dc66457724f to your computer and use it in GitHub Desktop.
Rubyでのベンチマークのやり方忘れそうなのでメモ
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
@y13i
Copy link
Author

y13i commented Jun 6, 2014

times何回も書くのアレなのでprocにすると良さげだとおもた

@y13i
Copy link
Author

y13i commented Jun 6, 2014

                           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