Skip to content

Instantly share code, notes, and snippets.

@ryanbriones
Created September 28, 2008 12:19
Show Gist options
  • Save ryanbriones/13447 to your computer and use it in GitHub Desktop.
Save ryanbriones/13447 to your computer and use it in GitHub Desktop.
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