Created
September 28, 2008 12:19
-
-
Save ryanbriones/13447 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" | |
TIMES = (ARGV[0] || 1_000_000).to_i | |
class A | |
def to_s | |
"foo" | |
end | |
end | |
class B | |
attr_accessor :a | |
end | |
Benchmark.bmbm do |x| | |
x.report("rescue") { TIMES.times {b = B.new; (b.a.to_s rescue nil)} } | |
x.report("check nil") { TIMES.times {b = B.new; b.a && b.a.to_s} } | |
x.report("terenary") { TIMES.times {b = B.new; (b.a ? b.a.to_s : nil)}} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment