Skip to content

Instantly share code, notes, and snippets.

@sgrif
Created February 5, 2015 17:51
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 sgrif/b86832786551aaee74de to your computer and use it in GitHub Desktop.
Save sgrif/b86832786551aaee74de to your computer and use it in GitHub Desktop.
def baz(x)
x + yield
end
def bar(&block)
baz(1, &block)
end
def foo(&block)
bar(&block)
end
def foo_with_manual_delegation
bar_with_manual_delegation { yield if block_given? }
end
def bar_with_manual_delegation
baz(1) { yield if block_given? }
end
require "benchmark/ips"
Benchmark.ips do |bm|
bm.report("&block") { foo { 1 } }
bm.report("manual") { foo_with_manual_delegation { 1 } }
bm.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment