-
-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b925f5f
commit 48e2a3e
Showing
3 changed files
with
60 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'benchmark/ips' | ||
|
||
def yield_control | ||
yield | ||
end | ||
|
||
def capture_block_and_yield(&block) | ||
yield | ||
end | ||
|
||
def capture_block_and_call(&block) | ||
block.call | ||
end | ||
|
||
Benchmark.ips do |x| | ||
x.report("yield ") do | ||
yield_control { } | ||
end | ||
|
||
x.report("capture block and yield") do | ||
capture_block_and_yield { } | ||
end | ||
|
||
x.report("capture block and call ") do | ||
capture_block_and_call { } | ||
end | ||
end | ||
|
||
__END__ | ||
|
||
This benchmark demonstrates that `yield` is much, much faster | ||
than capturing `&block` and calling it. In fact, the simple act | ||
of capturing `&block`, even if we don't later reference `&block`, | ||
incurs most of the cost, so we should avoid capturing blocks unless | ||
we absolutely need to. | ||
|
||
Calculating ------------------------------------- | ||
yield | ||
93.104k i/100ms | ||
capture block and yield | ||
52.682k i/100ms | ||
capture block and call | ||
51.115k i/100ms | ||
------------------------------------------------- | ||
yield | ||
5.161M (±10.6%) i/s - 25.231M | ||
capture block and yield | ||
1.141M (±22.0%) i/s - 5.426M | ||
capture block and call | ||
1.027M (±21.8%) i/s - 4.856M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters