Skip to content

Instantly share code, notes, and snippets.

@tomafro
Forked from defunkt/gist:160718
Created August 4, 2009 08:13
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 tomafro/161099 to your computer and use it in GitHub Desktop.
Save tomafro/161099 to your computer and use it in GitHub Desktop.
# 9 out of 10 microbenchmarks agree: implicit return smokes explicit return
require 'benchmark'
def explicit
return 1
end
def implicit
1
end
n = 10_000_000
Benchmark.bmbm do |x|
x.report("explicit") { n.times { explicit } }
x.report("implicit") { n.times { implicit } }
end
$ ruby test.rb
Rehearsal --------------------------------------------
explicit 4.090000 0.020000 4.110000 ( 4.143585)
implicit 2.610000 0.010000 2.620000 ( 2.631262)
----------------------------------- total: 6.730000sec
user system total real
explicit 4.100000 0.010000 4.110000 ( 4.805887)
implicit 2.630000 0.010000 2.640000 ( 2.660820)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment