Actions
Misc #10983
closedWhy blocks make Ruby methods 439% slower ?
Misc #10983:
Why blocks make Ruby methods 439% slower ?
Status:
Closed
Assignee:
-
Description
https://www.omniref.com/ruby/2.2.0/symbols/Proc/yield#annotation=4087638&line=711&hn=1
require 'benchmark/ips'
def block_call(&block)
block.call
end
def just_yield
yield
end
Benchmark.ips do |x|
x.report("call") do
block_call { 1 + 1 }
end
x.report("just yield") do
just_yield { 1 + 1 }
end
x.compare!
end
I run on Ruby 2.2.1
Updated by ko1 (Koichi Sasada) over 11 years ago
To capture local variables and so on (== an environment) for several frames is too heavy.
For example,
you need to capture all blocks' environments.
You don't need to capture environments only passing block.
So you need to say "passing block is faster" :)
BTW, I agree it is slow.
Improve the performance on this behavior will be great job.
Updated by nobu (Nobuyoshi Nakada) almost 11 years ago
- Description updated (diff)
Updated by Earlopain (Earlopain _) 3 days ago
- Status changed from Open to Closed
Today the difference is only ~1.25x slower, not nearly as bad as it used to be.
YJIT also improves both versions by at least 2x, altough it is able to optimize yield more than the explicit call.
Actions