Skip to content

Instantly share code, notes, and snippets.

@asterite
Last active August 29, 2015 14:14
Show Gist options
  • Save asterite/7267571938a96989f3b1 to your computer and use it in GitHub Desktop.
Save asterite/7267571938a96989f3b1 to your computer and use it in GitHub Desktop.
require "benchmark"
def foo(&block)
bar(&block)
end
def bar
yield
end
def foo2
bar2 do
yield
end
end
def bar2
yield
end
def foo3(&block)
bar3 do
yield
end
end
def bar3
yield
end
Benchmark.bmbm(20) do |bm|
bm.report("capture") do
1_000_000.times do
a = 0
foo do
a += 1
end
end
end
bm.report("non_capture") do
1_000_000.times do
a = 0
foo2 do
a += 1
end
end
end
bm.report("non_capture_with_name") do
1_000_000.times do
a = 0
foo3 do
a += 1
end
end
end
end
Rehearsal ---------------------------------------------------------
capture 0.760000 0.010000 0.770000 ( 0.762715)
non_capture 0.220000 0.000000 0.220000 ( 0.223030)
non_capture_with_name 0.820000 0.000000 0.820000 ( 0.829489)
------------------------------------------------ total: 1.810000sec
user system total real
capture 0.760000 0.010000 0.770000 ( 0.767258)
non_capture 0.220000 0.000000 0.220000 ( 0.220611)
non_capture_with_name 0.860000 0.000000 0.860000 ( 0.875678)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment