forked from weppos/whois
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bm_delegation_vs_inheritance.rb
150 lines (140 loc) · 3.19 KB
/
bm_delegation_vs_inheritance.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'benchmark'
require 'delegate'
class Foo < DelegateClass(String)
attr_reader :optional
def initialize(mandatory, optional = nil)
super(String.new(mandatory))
@optional = optional
end
end
class Bar < String
attr_reader :optional
def initialize(mandatory, optional = nil)
super(mandatory)
@optional = optional
end
end
class Baz
attr_reader :optional
def initialize(mandatory, optional = nil)
@string = mandatory.to_s
@optional = optional
end
def match(*args)
@string.match(*args)
end
def gsub(*args)
@string.gsub(*args)
end
def gsub!(*args)
@string.gsub!(*args)
end
end
class Option < Struct.new(:qui, :quo, :qua)
def to_s
"-" * 100
end
end
TIMES = 100_000
CONTENT = <<-LOREM
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.
LOREM
Benchmark.bmbm do |x|
x.report("inheritance =~") do
TIMES.times do
s = Foo.new(CONTENT, Option.new)
s =~ /Duis/
end
end
x.report("delegation =~") do
TIMES.times do
s = Bar.new(CONTENT, Option.new)
s =~ /Duis/
end
end
x.report("composition =~") do
# TIMES.times do
# s = Baz.new(CONTENT, Option.new)
# s =~ /Duis/
# end
end
x.report("inheritance match") do
TIMES.times do
s = Foo.new(CONTENT, Option.new)
s.match(/Status:/)
end
end
x.report("delegation match") do
TIMES.times do
s = Bar.new(CONTENT, Option.new)
s.match(/Status:/)
end
end
x.report("composition match") do
TIMES.times do
s = Baz.new(CONTENT, Option.new)
s.match(/Status:/)
end
end
x.report("inheritance gsub") do
TIMES.times do
s = Foo.new(CONTENT, Option.new)
s.gsub(/ /, '_')
end
end
x.report("delegation gsub") do
TIMES.times do
s = Bar.new(CONTENT, Option.new)
s.gsub(/ /, '_')
end
end
x.report("composition gsub") do
TIMES.times do
s = Baz.new(CONTENT, Option.new)
s.gsub(/ /, '_')
end
end
x.report("inheritance gsub!") do
TIMES.times do
s = Foo.new(CONTENT, Option.new)
s.gsub!(/ /, '_')
end
end
x.report("delegation gsub!") do
TIMES.times do
s = Bar.new(CONTENT, Option.new)
s.gsub!(/ /, '_')
end
end
x.report("composition gsub!") do
TIMES.times do
s = Baz.new(CONTENT, Option.new)
s.gsub!(/ /, '_')
end
end
x.report("inheritance option") do
TIMES.times do
s = Foo.new(CONTENT, Option.new)
s.optional.to_s
end
end
x.report("delegation option") do
TIMES.times do
s = Bar.new(CONTENT, Option.new)
s.optional.to_s
end
end
x.report("composition option") do
TIMES.times do
s = Baz.new(CONTENT, Option.new)
s.optional.to_s
end
end
end