Skip to content

Instantly share code, notes, and snippets.

@XrXr
Created May 16, 2019 21:45
Show Gist options
  • Save XrXr/0e81775c2c430a73588b381fdd2fc47d to your computer and use it in GitHub Desktop.
Save XrXr/0e81775c2c430a73588b381fdd2fc47d to your computer and use it in GitHub Desktop.
Block-pass benchmark
require 'bundler/inline'
require 'benchmark/ips'
N = 10_000
def without
yield
end
def with_dot_call(&block)
block.call
end
def with_but_yield(&block)
yield
end
Benchmark.ips do |x|
x.report("call without & argument") do
N.times do
without {}
end
end
x.report("call with explicit & argument and .call") do
N.times do
with_dot_call {}
end
end
x.report("call with explicit & argument and yield") do
N.times do
with_but_yield {}
end
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment