Skip to content

Instantly share code, notes, and snippets.

@mattetti
Created September 5, 2011 18:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattetti/1195674 to your computer and use it in GitHub Desktop.
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.
$ 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
@bruce
Copy link

bruce commented Sep 5, 2011

Yup, it's slower. Too slow or significant enough? That's a matter of context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment