Skip to content

Instantly share code, notes, and snippets.

@donigian
Created June 9, 2015 07:37
Show Gist options
  • Save donigian/66999e63a471fa624f57 to your computer and use it in GitHub Desktop.
Save donigian/66999e63a471fa624f57 to your computer and use it in GitHub Desktop.
Ruby Benchmark Howto
require "benchmark"
def calculation_with_explicit_block_passing(a, b, operation)
operation.call(a, b)
end
def calculation_with_implicit_block_passing(a, b)
yield(a, b)
end
Benchmark.bmbm(10) do |report|
report.report("explicit") do
addition = lambda { |a, b| a + b }
1000.times { calculation_with_explicit_block_passing(5, 5, addition) }
end
report.report("implicit") do
1000.times { calculation_with_implicit_block_passing(5, 5) { |a, b| a + b } }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment