Skip to content

Instantly share code, notes, and snippets.

@composerinteralia
Last active October 29, 2017 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save composerinteralia/81feb92675b0c9a745c6f04e3da57a70 to your computer and use it in GitHub Desktop.
Save composerinteralia/81feb92675b0c9a745c6f04e3da57a70 to your computer and use it in GitHub Desktop.
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