Skip to content

Instantly share code, notes, and snippets.

@simonoff
Forked from fxposter/gist:a8624290de55d0dc714a
Last active August 29, 2015 14:04
Show Gist options
  • Save simonoff/0a77d50fd4f8dab33457 to your computer and use it in GitHub Desktop.
Save simonoff/0a77d50fd4f8dab33457 to your computer and use it in GitHub Desktop.
require 'benchmark'
def one
yield 1
end
def a
one { |x| yield x }
end
def b(&block)
one(&block)
end
def c
one(&Proc.new)
end
n = 1_000_000
Benchmark.bmbm do |x|
x.report("&block") do
n.times { b { |x| x } }
end
x.report("yield") do
n.times { a { |x| x } }
end
x.report("&Proc.new") do
n.times { c { |x| x } }
end
end
# Rehearsal ---------------------------------------------
# &block 1.010000 0.020000 1.030000 ( 1.034047)
# yield 0.190000 0.000000 0.190000 ( 0.190613)
# &Proc.new 1.110000 0.010000 1.120000 ( 1.123012)
# ------------------------------------ total: 2.340000sec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment