blob: f0d0a29b6b4178787492f16312523a6a7d5bf8b7 (
plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
require 'benchmark'
$max_trial = 10
def bm msg
real = (1..$max_trial).map{
Benchmark.measure{
yield
}.real
}.min
puts "#{msg}\t#{real}"
real
end
def bench1
max = 10_000_000
cm = bm 'c-method' do
max.times{
null_func()
}
end
ri = bm 'ricsin' do
max.times{
__C__('')
}
end
lo = bm 'loop' do
max.times{
}
end
puts [cm-lo, ri-lo].join("\t")
end
__Cdecl__ %q{
#define FIX_PP(i) i += 2
#define FIX_CMP_LT(x, y) ((SIGNED_VALUE)(x) < (SIGNED_VALUE)(y))
}
class Array
def each_ricsin
return self.enum_for(:each) unless block_given?
i = 0
e = nil
#C for (; FIX2LONG(i) < RARRAY_LEN(self); FIX_PP(i)) {
#C e = RARRAY_PTR(self)[FIX2LONG(i)];
yield e
#C }
self
end
def each_ruby
i = 0
while i < self.length
yield self[i]
i = i.succ
end
end
end
class Fixnum
alias each times
def each_ricsin
return self.enum_for(:times) unless block_given?
i = 0
#C for (; FIX_CMP_LT(i, self); FIX_PP(i)) {
yield i
#C }
i
end
def each_ruby
i = 0
while i < self
yield i
i = i.succ
end
i
end
end
class Range
def each_ricsin
return self.enum_for(:each) unless block_given?
i = self.first
e = self.last
e += 1 unless self.exclude_end?
#C for (; FIX_CMP_LT(i, e); FIX_PP(i)) {
yield(i)
#C }
self
end
def each_ruby
i = self.first
e = self.last
e += 1 if self.exclude_end?
while i<e
yield
i = i.succ
end
self
end
end
def bench2
max = 10_000_000
[[Array.new(max), :each], [(1..max), :each], [max, :times]].each{|obj, mid|
begin
bm 'cfunc' do
obj.each{}
end
bm 'ricsin' do
obj.each_ricsin{}
end
bm 'ruby' do
obj.each_ruby{}
end
bm 'cfunc+cb' do
obj.each(&__Cb__(''))
end
bm 'ricsin+cb' do
obj.each_ricsin(&__Cb__(''))
end
bm 'ruby+cb' do
obj.each_ruby(&__Cb__(''))
end
ensure
#
end
}
end
#bench1()
bench2()
__END__
###
require 'benchmark'
max = 10000000
obj = Array.new(max)
Benchmark.bm{|x|
x.report("orig "){
obj.each{|e|}
}
x.report("ricsin"){
obj.each2{|e|}
}
x.report("pure "){
obj.each3{|e|}
}
} if false
obj = (1..max)
Benchmark.bm{|x|
x.report("orig "){
obj.each{|e|}
}
x.report("ricsin"){
obj.each2{|e|}
}
x.report("pure "){
obj.each3{|e|}
}
} if false
Benchmark.bm{|x|
x.report("orig "){
max.times{|i|}
}
x.report("ricsin"){
max.times2{|i|}
}
x.report("pure "){
max.times3{|i|}
}
x.report("cblock"){
max.times(&__Cb__(""))
}
x.report("cblock"){
max.times2(&__Cb__(""))
}
x.report("cblock"){
max.times3(&__Cb__(""))
}
}
__END__
ruby 1.9.0 (2008-09-19 revision 0) [i686-linux]
user system total real
orig 2.860000 0.000000 2.860000 ( 2.864203)
ricsin 2.160000 0.000000 2.160000 ( 2.161214)
pure 5.130000 0.000000 5.130000 ( 5.138235)
user system total real
orig 2.810000 0.000000 2.810000 ( 2.822292)
ricsin 2.070000 0.000000 2.070000 ( 2.066880)
pure 2.790000 0.000000 2.790000 ( 2.787441)
user system total real
orig 2.700000 0.000000 2.700000 ( 2.697477)
ricsin 2.070000 0.000000 2.070000 ( 2.078570)
pure 2.800000 0.000000 2.800000 ( 2.794948)
ruby 1.9.0 (2008-09-20 revision 12) [x86_64-linux]
user system total real
orig 0.830000 0.000000 0.830000 ( 0.825043)
ricsin 0.540000 0.010000 0.550000 ( 0.545134)
pure 1.310000 0.000000 1.310000 ( 1.313828)
user system total real
orig 0.850000 0.000000 0.850000 ( 0.855513)
ricsin 0.560000 0.000000 0.560000 ( 0.554663)
pure 0.660000 0.000000 0.660000 ( 0.663112)
user system total real
orig 0.860000 0.000000 0.860000 ( 0.854639)
ricsin 0.540000 0.000000 0.540000 ( 0.547628)
pure 0.790000 0.000000 0.790000 ( 0.787023)
Benchmark.bm{|x|
x.report("orig "){
max.times{|i|}
}
x.report("ricsin"){
max.times2{|i|}
}
x.report("pure "){
max.times3{|i|}
}
x.report("cblock"){
max.times(&__Cb__(""))
}
x.report("cblock"){
max.times2(&__Cb__(""))
}
x.report("cblock"){
max.times3(&__Cb__(""))
}
}
user system total real
orig 2.760000 0.000000 2.760000 ( 2.757706)
ricsin 2.230000 0.000000 2.230000 ( 2.236137)
pure 3.130000 0.000000 3.130000 ( 3.159513)
cblock 1.000000 0.000000 1.000000 ( 1.004778)
cblock 1.960000 0.000000 1.960000 ( 1.963191)
cblock 2.740000 0.000000 2.740000 ( 2.747436)
|