Skip to content

Instantly share code, notes, and snippets.

@defunkt
Created August 3, 2009 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save defunkt/160718 to your computer and use it in GitHub Desktop.
Save defunkt/160718 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