Created
May 10, 2011 14:49
-
-
Save rondevera/964615 to your computer and use it in GitHub Desktop.
#define_method vs ActiveSupport::StringInquirer
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 ---------------------------------------------- | |
#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) |
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
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