Created
November 10, 2011 17:11
-
-
Save marshluca/1355440 to your computer and use it in GitHub Desktop.
Explicit vs implicit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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