Created
January 29, 2011 02:12
-
-
Save thedarkone/801424 to your computer and use it in GitHub Desktop.
This file contains 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" | |
LITERALS = { | |
nil => nil, 'nil' => nil, 'null' => nil, '' => nil, | |
'true' => true, | |
'false' => false, | |
'blank' => :blank?, | |
'empty' => :empty? | |
} | |
def via_literal(key) | |
LITERALS[key] if LITERALS.key?(key) | |
end | |
def via_case(key) | |
case key | |
when nil, 'nil', 'null', '' | |
nil | |
when 'true' | |
true | |
when 'false' | |
false | |
when 'blank' | |
:blank? | |
when 'empty' | |
:empty? | |
end | |
end | |
TESTS = 1_000_000 | |
Benchmark.bmbm do |results| | |
results.report('via_case(nil)') do | |
TESTS.times { via_case(nil) } | |
end | |
results.report('via_literal(nil)') do | |
TESTS.times { via_literal(nil) } | |
end | |
results.report('via_case("null")') do | |
TESTS.times { via_case("null") } | |
end | |
results.report('via_literal("null")') do | |
TESTS.times { via_literal("null") } | |
end | |
results.report('via_case("nomatch")') do | |
TESTS.times { via_case("nomatch") } | |
end | |
results.report('via_literal("nomatch")') do | |
TESTS.times { via_literal("nomatch") } | |
end | |
end |
This file contains 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
Rehearsal ---------------------------------------------------------- | |
via_case(nil) 0.260000 0.000000 0.260000 ( 0.296114) | |
via_literal(nil) 0.530000 0.000000 0.530000 ( 0.552677) | |
via_case("null") 0.750000 0.000000 0.750000 ( 0.761376) | |
via_literal("null") 0.570000 0.000000 0.570000 ( 0.646138) | |
via_case("nomatch") 1.610000 0.000000 1.610000 ( 1.669481) | |
via_literal("nomatch") 0.390000 0.010000 0.400000 ( 0.430214) | |
------------------------------------------------- total: 4.120000sec | |
user system total real | |
via_case(nil) 0.260000 0.000000 0.260000 ( 0.263596) | |
via_literal(nil) 0.520000 0.000000 0.520000 ( 0.530466) | |
via_case("null") 0.750000 0.000000 0.750000 ( 0.772132) | |
via_literal("null") 0.560000 0.000000 0.560000 ( 0.582794) | |
via_case("nomatch") 1.610000 0.000000 1.610000 ( 1.803847) | |
via_literal("nomatch") 0.390000 0.000000 0.390000 ( 0.395079) |
This file contains 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
Rehearsal ---------------------------------------------------------- | |
via_case(nil) 0.140000 0.000000 0.140000 ( 0.146629) | |
via_literal(nil) 0.210000 0.000000 0.210000 ( 0.210843) | |
via_case("null") 0.530000 0.000000 0.530000 ( 0.561717) | |
via_literal("null") 0.350000 0.000000 0.350000 ( 0.372644) | |
via_case("nomatch") 1.120000 0.010000 1.130000 ( 1.135756) | |
via_literal("nomatch") 0.260000 0.000000 0.260000 ( 0.271421) | |
------------------------------------------------- total: 2.620000sec | |
user system total real | |
via_case(nil) 0.140000 0.000000 0.140000 ( 0.174764) | |
via_literal(nil) 0.200000 0.000000 0.200000 ( 0.280553) | |
via_case("null") 0.530000 0.000000 0.530000 ( 0.570970) | |
via_literal("null") 0.360000 0.000000 0.360000 ( 0.364196) | |
via_case("nomatch") 1.110000 0.000000 1.110000 ( 1.165235) | |
via_literal("nomatch") 0.250000 0.000000 0.250000 ( 0.293636) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment