-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.rb
247 lines (222 loc) · 4.74 KB
/
bench.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
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
# A regression performance suite for MacRuby (which doesn't really catch
# regressions yet).
#
# Run with:
# $ ./miniruby -I./lib bench.rb
#
# The same script can also be executed using /usr/bin/ruby or ruby19 to
# compare the implementations.
def fib(n)
if n < 3
1
else
fib(n-1) + fib(n-2)
end
end
def tak(x, y, z)
unless y < x
z
else
tak(tak(x-1, y, z),
tak(y-1, z, x),
tak(z-1, x, y))
end
end
def tarai(x, y, z)
if x <= y
then y
else tarai(tarai(x-1, y, z),
tarai(y-1, z, x),
tarai(z-1, x, y))
end
end
def ack(m, n)
if m == 0 then
n + 1
elsif n == 0 then
ack(m - 1, 1)
else
ack(m - 1, ack(m, n - 1))
end
end
def tail(n)
return if n == 0
tail(n-1)
end
class Class1
def method1; end
def method2(x); x; end
def yield1(n); i=0; while i<n; yield; i+=1; end; end
def yield2; yield; end
end
class Class2 < Class1
def method1; super; end
def method2(x); super; end
end
def bench_ivar_read(n)
@obj = 1
i=0; while i<n; i+=@obj; end
end
def bench_ivar_write(n)
@obj = 0
i=0; while i<n; i+=@obj+1; end
end
ConstantOne = 1
require 'benchmark'
Benchmark.bm(30) do |bm|
# Fixnum arithmetic.
bm.report('10 fib(30)') do
i=0; while i<10; fib(30); i+=1; end
end
bm.report('10 fib(35)') do
i=0; while i<10; fib(35); i+=1; end
end
bm.report('tak') { tak(18,9,0) }
bm.report('tarai') { tarai(12,6,0) }
if RUBY_VERSION.to_f > 1.8
# Ruby 1.8 is too weak for this benchmark.
bm.report('ackermann') { ack(3,9) }
end
# Loops.
bm.report('10000000 times loop') do
3000000.times {}
end
bm.report('30000000 times loop') do
30000000.times {}
end
bm.report('10000000 while loop') do
i=0; while i<10000000; i+=1; end
end
bm.report('60000000 while loop') do
i=0; while i<60000000; i+=1; end
end
# Messages.
bm.report('30000000 msg w/ 0 arg') do
o = Class1.new
i=0; while i<10000000; o.method1; o.method1; o.method1; i+=1; end
end
bm.report('30000000 msg w/ 1 arg') do
o = Class1.new
i=0; while i<10000000; o.method2(i); o.method2(i); o.method2(i); i+=1; end
end
bm.report('10000000 super w/ 0 arg') do
o = Class2.new
i=0; while i<10000000; o.method1; i+=1; end
end
bm.report('10000000 super w/ 1 arg') do
o = Class2.new
i=0; while i<10000000; o.method2(i); i+=1; end
end
bm.report('10000000 #send') do
o = Class1.new
i=0; while i<10000000; o.send(:method1); i+=1; end
end
bm.report('30000000 tail calls') do
tail(30000000)
end
# Instance variables.
bm.report('10000000 ivar read') { bench_ivar_read(10000000) }
bm.report('30000000 ivar read') { bench_ivar_read(30000000) }
bm.report('10000000 ivar write') { bench_ivar_write(10000000) }
bm.report('30000000 ivar write') { bench_ivar_write(30000000) }
# Const lookup.
bm.report('30000000 const') do
i=0; while i<30000000; i+=ConstantOne; end
end
# Blocks
bm.report('30000000 yield') do
o = Class1.new
o.yield1(30000000) {}
end
bm.report('30000000 msg w/ block+yield') do
o = Class1.new
i=0; while i<30000000; o.yield2 {}; i+=1; end
end
bm.report('30000000 Proc#call') do
o = proc {}
i=0; while i<30000000; o.call; i+=1; end
end
bm.report('30000000 dvar write') do
i=0
30000000.times { i=1 }
end
# Eval
bm.report('1000 eval') do
i=0
s = "#{1+1}+#{20+20}"
while i<1000
eval(s)
i+=1
end
end
bm.report('30000000 binding-var write') do
i=0
eval('while i<30000000; i+=1; end')
end
# Break
bm.report('30000000 while break') do
i=0; while i<30000000; while true; i+=1; break; end; end
end
bm.report('10000000 block break') do
i=0; while i<10000000; 1.times { i+=1; break }; end
end
# Next
bm.report('30000000 while next') do
i=0; while i<30000000; i+=1; next; exit; end
end
bm.report('10000000 block next') do
i=0; while i<10000000; 1.times { i+=1; next; exit; }; end
end
# Exception handlers.
bm.report('60000000 begin w/o exception') do
i=0
while i<60000000
begin
i+=1
rescue
end
end
end
bm.report('60000000 ensure w/o exception') do
i=0
while i<60000000
begin
begin
ensure
i+=1
end
ensure
i+=1
end
end
end
bm.report('50000 raise') do
i=0
while i<50000
begin
raise
rescue
i+=1
end
end
end
# Method
bm.report('3000000 Method#call w/ 0 arg') do
o = Class1.new
m = o.method(:method1)
i=0
while i<3000000
m.call
i+=1
end
end
bm.report('3000000 Method#call w/ 1 arg') do
o = Class1.new
m = o.method(:method2)
i=0
while i<3000000
m.call(i)
i+=1
end
end
end