Created
July 3, 2015 11:21
-
-
Save nowlinuxing/bae80a0cd7784e577bab to your computer and use it in GitHub Desktop.
Proc#call vs. yield
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
# Proc#call vs. yield - PB memo <http://d.hatena.ne.jp/nagachika/20111105/proc_call_versus_yield> | |
require 'benchmark' | |
n = 1_000_000 | |
def m1 | |
yield :m1 | |
end | |
def m2(&block) | |
block.call(:m2) | |
end | |
Benchmark.bm do |b| | |
b.report("m1") { n.times.each { |i| m1 {} } } | |
b.report("m2") { n.times.each { |i| m2 {} } } | |
end | |
=begin | |
$ ruby -v | |
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin13] | |
$ ruby test.rb | |
user system total real | |
m1 0.150000 0.000000 0.150000 ( 0.155446) | |
m2 0.720000 0.010000 0.730000 ( 0.730200) | |
$ | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment