Last active
October 29, 2017 16:26
-
-
Save composerinteralia/81feb92675b0c9a745c6f04e3da57a70 to your computer and use it in GitHub Desktop.
Benchmarking yield, call, instance_eval
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
require 'benchmark' | |
def yielder | |
yield | |
end | |
def caller(&block) | |
block.call | |
end | |
def instance_evaluator(&block) | |
instance_eval(&block) | |
end | |
TIMES = 1000000 | |
Benchmark.bm do |bm| | |
bm.report("yield") { TIMES.times { yielder {} } } | |
bm.report("call") { TIMES.times { caller {} } } | |
bm.report("instance_eval") { TIMES.times { instance_evaluator {} } } | |
end | |
# user system total real | |
# yield 0.080000 0.000000 0.080000 (0.082796) | |
# call 0.560000 0.000000 0.560000 (0.561398) | |
# instance_eval 0.660000 0.010000 0.670000 (0.670522) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment