Created
March 28, 2016 20:00
-
-
Save youchan/a00fa476768b2b3dc907 to your computer and use it in GitHub Desktop.
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
# Benchmark for compare Kernel#proc vs block argument (and vs using yield) | |
require 'benchmark' | |
def receiver | |
yield | |
end | |
def send_using_proc | |
receiver &proc if block_given? | |
end | |
def send_block_arg(&block) | |
receiver &block if block_given? | |
end | |
def send_using_yield | |
receiver(&proc { yield }) if block_given? | |
end | |
Benchmark.bm do |bm| | |
bm.report("use proc") { 1000000.times { send_using_proc { true } } } | |
bm.report("block arg") { 1000000.times { send_block_arg { true } } } | |
bm.report("use yield") { 1000000.times { send_using_yield { true } } } | |
end | |
# ******************** result ******************** | |
# | |
# user system total real | |
# use proc 0.670000 0.000000 0.670000 ( 0.706581) | |
# block arg 0.690000 0.010000 0.700000 ( 0.712470) | |
# use yield 1.170000 0.000000 1.170000 ( 1.234635) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment