Closed st0012 closed 6 years ago
Hi. Pardon me, as I am helping @JuanitoFatas with the PRs.
Would this actually be more about comparing the instantiation of block
vs Proc
, rather than either being a parameter to a method? Just trying to dig into the atomic action that better defines this benchmark. Thanks!
@st0012 In your example you are benchmarking: "accessing a block passed in" v.s. "instantiating Proc instance".
Should this benchmark be something like this?
require "benchmark/ips"
def block &block
block.call
end
def proclike proc
proc.call
end
Benchmark.ips do |x|
x.report("&block") { block { 1 + 1 } }
x.report("->") { proclike -> { 1 + 1 } }
x.report("Lambda") { proclike lambda { 1 + 1 } }
x.report("Proc.new") { proclike Proc.new { 1 + 1 } }
x.compare!
end
I think that if the benefit is below 12% is not worth to mention, the README says so
There are two ways to access block argument, one is using
&block
.Another is using
Proc.new
inside the method.And the benchmark result was