Skip to content

Instantly share code, notes, and snippets.

@tkareine
Created January 12, 2014 19:06
Show Gist options
  • Save tkareine/8388957 to your computer and use it in GitHub Desktop.
Save tkareine/8388957 to your computer and use it in GitHub Desktop.
require 'benchmark'
def with_greet(name)
yield "hi, #{name}"
end
def with_greet_jack
with_greet("jack") { |greet| yield greet }
end
def with_greet_jill(&block)
with_greet("jill", &block)
end
N = 1_000_000
Benchmark.bmbm do |x|
x.report("jack") do
N.times { with_greet_jack { |greet| greet } }
end
x.report("jill") do
N.times { with_greet_jill { |greet| greet } }
end
end
# On 2.6 GHz Intel Core i7, 16 GB RAM
# Ruby MRI 2.1.0, Mac OS X 10.9.1
#
# Rehearsal ----------------------------------------
# jack 0.310000 0.000000 0.310000 ( 0.307267)
# jill 0.810000 0.000000 0.810000 ( 0.818499)
# ------------------------------- total: 1.120000sec
#
# user system total real
# jack 0.300000 0.000000 0.300000 ( 0.305173)
# jill 0.810000 0.000000 0.810000 ( 0.810883)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment