Skip to content

Instantly share code, notes, and snippets.

@dscataglini
Created December 12, 2008 23:15
Show Gist options
  • Save dscataglini/35322 to your computer and use it in GitHub Desktop.
Save dscataglini/35322 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'andand'
require 'benchmark'
include Benchmark
t = 1_000_000
class A
attr_accessor :foo
end
class B
def bar
"bar"
end
end
a = A.new
a.foo = B.new
b = A.new
bm(30) do |x|
x.report("andand"){ t.times{ a.foo.andand.bar }}
x.report("if"){ t.times{ a.foo.bar if a.foo }}
x.report("andand with nil"){ t.times{ b.foo.andand.bar }}
x.report("if with nil"){ t.times{ b.foo.bar if b.foo }}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment