Skip to content

Instantly share code, notes, and snippets.

@rondevera
Created May 10, 2011 14:49
Show Gist options
  • Save rondevera/964615 to your computer and use it in GitHub Desktop.
Save rondevera/964615 to your computer and use it in GitHub Desktop.
#define_method vs ActiveSupport::StringInquirer
Rehearsal ----------------------------------------------
#one? 0.000000 0.000000 0.000000 ( 0.001843)
#one? 0.000000 0.000000 0.000000 ( 0.002105)
#kind.one? 0.010000 0.000000 0.010000 ( 0.005502)
------------------------------------- total: 0.010000sec
user system total real
#one? 0.000000 0.000000 0.000000 ( 0.002660)
#one? 0.000000 0.000000 0.000000 ( 0.002275)
#kind.one? 0.000000 0.000000 0.000000 ( 0.003826)
class A
attr_accessor :kind
def initialize
self.kind = 'one'
end
def one?
self.kind == kind.to_s
end
end
class B
attr_accessor :kind
def initialize
self.kind = 'one'
end
%w[one two three four five].each do |kind|
define_method("#{kind}?") do
self.kind == kind.to_s
end
end
end
class C
attr_accessor :kind
def initialize
self.kind = ActiveSupport::StringInquirer.new('one')
end
end
Benchmark.bmbm do |x|
x.report('#one?') do
1_000.times { A.new.one? }
end
x.report('#one?') do
1_000.times { B.new.one? }
end
x.report('#kind.one?') do
1_000.times { C.new.kind.one? }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment