-
-
Save dstrelau/965007 to your computer and use it in GitHub Desktop.
#define_method vs ActiveSupport::StringInquirer
This file contains hidden or 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(kind) | |
self.kind = kind | |
end | |
def one? ; self.kind == 'one' ; end | |
def two? ; self.kind == 'two' ; end | |
def three? ; self.kind == 'three' ; end | |
def three? ; self.kind == 'four' ; end | |
def five? ; self.kind == 'five' ; end | |
end |
This file contains hidden or 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 B | |
attr_accessor :kind | |
def initialize(kind) | |
self.kind = kind | |
end | |
%w[one two three four five].each do |kind| | |
define_method("#{kind}?") do | |
self.kind == kind.to_s | |
end | |
end | |
end |
This file contains hidden or 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 C | |
attr_accessor :kind | |
def initialize(kind) | |
self.kind = ActiveSupport::StringInquirer.new(kind) | |
end | |
end |
This file contains hidden or 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
# ruby -rubygems -I. setup.rb | |
################################################################## | |
# ruby 1.8.7 (2010-12-23 patchlevel 330) [x86_64-darwin10.5.0] | |
################################################################## | |
Rehearsal -------------------------------------------------------- | |
A (explicit methods) 0.150000 0.030000 0.180000 ( 0.176056) | |
B (define_method) 0.180000 0.040000 0.220000 ( 0.213807) | |
C (string inquirer) 0.070000 0.030000 0.100000 ( 0.098579) | |
----------------------------------------------- total: 0.500000sec | |
user system total real | |
A (explicit methods) 0.150000 0.020000 0.170000 ( 0.176828) | |
B (define_method) 0.180000 0.040000 0.220000 ( 0.217881) | |
C (string inquirer) 0.060000 0.020000 0.080000 ( 0.094166) | |
################################################################## | |
# ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0] | |
################################################################## | |
Rehearsal -------------------------------------------------------- | |
A (explicit methods) 0.460000 0.140000 0.600000 ( 0.596940) | |
B (define_method) 0.370000 0.090000 0.460000 ( 0.467380) | |
C (string inquirer) 0.200000 0.070000 0.270000 ( 0.269189) | |
----------------------------------------------- total: 1.330000sec | |
user system total real | |
A (explicit methods) 0.450000 0.140000 0.590000 ( 0.590053) | |
B (define_method) 0.370000 0.100000 0.470000 ( 0.473767) | |
C (string inquirer) 0.200000 0.060000 0.260000 ( 0.264523) | |
################################################################## | |
# jruby 1.6.0 (ruby 1.8.7 patchlevel 330) (2011-03-15 f3b6154) | |
# (Java HotSpot(TM) 64-Bit Server VM 1.6.0_24) [darwin-x86_64-java] | |
################################################################## | |
Rehearsal -------------------------------------------------------- | |
A (explicit methods) 0.622000 0.000000 0.622000 ( 0.621000) | |
B (define_method) 0.494000 0.000000 0.494000 ( 0.494000) | |
C (string inquirer) 0.221000 0.000000 0.221000 ( 0.221000) | |
----------------------------------------------- total: 1.337000sec | |
user system total real | |
A (explicit methods) 0.384000 0.000000 0.384000 ( 0.384000) | |
B (define_method) 0.456000 0.000000 0.456000 ( 0.456000) | |
C (string inquirer) 0.199000 0.000000 0.199000 ( 0.199000) | |
################################################################## | |
# rubinius 1.2.4dev | |
# (1.8.7 090c86c6 yyyy-mm-dd) [x86_64-apple-darwin10.7.0] | |
################################################################## | |
Rehearsal -------------------------------------------------------- | |
A (explicit methods) 0.461322 0.051510 0.512832 ( 0.514030) | |
B (define_method) 0.387867 0.050668 0.438535 ( 0.441115) | |
C (string inquirer) 0.287570 0.045437 0.333007 ( 0.333381) | |
----------------------------------------------- total: 1.284374sec | |
user system total real | |
A (explicit methods) 0.417872 0.051728 0.469600 ( 0.469912) | |
B (define_method) 0.435688 0.050810 0.486498 ( 0.488859) | |
C (string inquirer) 0.335798 0.045136 0.380934 ( 0.380966) |
This file contains hidden or 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' | |
require 'active_support/string_inquirer' | |
TIMES = 1_000 | |
Benchmark.bmbm do |x| | |
x.report('A (explicit methods)') do | |
TIMES.times { load 'a.rb' } | |
end | |
x.report('B (define_method)') do | |
TIMES.times { load 'b.rb' } | |
end | |
x.report('C (string inquirer)') do | |
TIMES.times { load 'c.rb' } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment