Created
September 5, 2011 18:58
-
-
Save mattetti/1195674 to your computer and use it in GitHub Desktop.
confirm that using &block does an extra allocation making it almost 7X slower than using yield.
This file contains hidden or 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
$ ruby --version | |
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0] | |
$ time ruby -e "def foo(&block); block.call; end; 2_000_000.times{ foo{1+1} };" | |
real 0m1.556s | |
user 0m1.482s | |
sys 0m0.075s | |
$ time ruby -e "def foo; yield; end; 2_000_000.times{ foo{1+1} };" | |
real 0m0.233s | |
user 0m0.229s | |
sys 0m0.003s | |
$ time ruby -e "def foo(&block); block.call if block; end; 2_000_000.times{ foo{1+1} };" | |
real 0m1.609s | |
user 0m1.533s | |
sys 0m0.077s | |
$ time ruby -e "def foo; yield if block_given?; end; 2_000_000.times{ foo{1+1} };" | |
real 0m0.342s | |
user 0m0.338s | |
sys 0m0.003s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yup, it's slower. Too slow or significant enough? That's a matter of context.