Skip to content

Instantly share code, notes, and snippets.

@marshluca
Created November 10, 2011 17:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marshluca/1355440 to your computer and use it in GitHub Desktop.
Save marshluca/1355440 to your computer and use it in GitHub Desktop.
Explicit vs implicit
require 'benchmark'
def explicit
return "TEST"
end
def implicit
"TEST"
end
def explicit_non_eval
return 'TEST'
end
def implicit_non_eval
'TEST'
end
def explicit_symbol
return :TEST
end
def implicit_symbol
:TEST
end
n = 100_000_000
Benchmark.bmbm do |x|
x.report("Explicit \"\" return") { n.times { explicit } }
x.report("Implicit \"\" return") { n.times { implicit } }
x.report("Explicit '' return") { n.times { explicit_non_eval } }
x.report("Implicit '' return") { n.times { implicit_non_eval } }
x.report("Explicit symbol return") { n.times { explicit_symbol } }
x.report("Implicit symbol return") { n.times { implicit_symbol } }
end
the results
Rehearsal ----------------------------------------------------------
Explicit "" return 37.810000 0.000000 37.810000 ( 37.889241)
Implicit "" return 36.300000 0.000000 36.300000 ( 36.310153)
Explicit '' return 36.460000 0.000000 36.460000 ( 36.476939)
Implicit '' return 36.520000 0.010000 36.530000 ( 36.541361)
Explicit symbol return 14.330000 0.000000 14.330000 ( 14.352004)
Implicit symbol return 14.330000 0.000000 14.330000 ( 14.322934)
----------------------------------------------- total: 175.760000sec
user system total real
Explicit "" return 37.620000 0.000000 37.620000 ( 37.680337)
Implicit "" return 36.480000 0.000000 36.480000 ( 36.504694)
Explicit '' return 36.560000 0.000000 36.560000 ( 36.574798)
Implicit '' return 36.100000 0.010000 36.110000 ( 36.111328)
Explicit symbol return 14.770000 0.000000 14.770000 ( 14.812874)
Implicit symbol return 14.710000 0.000000 14.710000 ( 14.829556)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment