Instantly share code, notes, and snippets.

Embed
What would you like to do?
Benchmarking yield, call, instance_eval
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