Skip to content

Commit 1a8f63b

Browse files
committed
* ricsin/benchmark.rcb: add benchmarks.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ricsin@19616 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent ddbf9c4 commit 1a8f63b

File tree

1 file changed

+102
-2
lines changed

1 file changed

+102
-2
lines changed

ricsin/benchmark.rcb

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,109 @@ def bench2
139139
}
140140
end
141141

142-
#bench1()
143-
bench2()
142+
require 'matrix'
143+
144+
class Matrix
145+
def *(m) # m is matrix or vector or number
146+
case(m)
147+
when Numeric
148+
rows = @rows.collect {
149+
|row|
150+
row.collect {
151+
|e|
152+
e * m
153+
}
154+
}
155+
return Matrix.rows(rows, false)
156+
when Vector
157+
m = Matrix.column_vector(m)
158+
r = self * m
159+
return r.column(0)
160+
when Matrix
161+
Matrix.Raise ErrDimensionMismatch if column_size != m.row_size
162+
163+
if true
164+
rs = row_size
165+
cs = column_size
166+
mary = m.instance_eval{@rows}
167+
sary = @rows
168+
rows = __C__ %q{
169+
int row_size = FIX2INT(rs);
170+
int column_size = FIX2INT(cs);
171+
int ci=0, cj=0, ck=0;
172+
VALUE crows = rb_ary_new();
173+
for (ci=0; ci<row_size; ci++) {
174+
VALUE ccols = rb_ary_new();
175+
for (cj=0; cj<column_size; cj++) {
176+
int cvij = 0;
177+
double d = 0;
178+
for (ck=0; ck<column_size; ck++) {
179+
#define REF(m, i, j) RARRAY_PTR(RARRAY_PTR(m)[i])[j]
180+
VALUE a = REF(sary, ci, ck);
181+
VALUE b = REF(mary, ck, cj);
182+
183+
if (FIXNUM_P(a) && FIXNUM_P(b)) {
184+
cvij += FIX2INT(a) * FIX2INT(b);
185+
}
186+
else if (BUILTIN_TYPE(a) == T_FLOAT &&
187+
BUILTIN_TYPE(b) == T_FLOAT) {
188+
d += RFLOAT_VALUE(a) * RFLOAT_VALUE(b);
189+
}
190+
else {
191+
rb_bug("unsupported\n");
192+
}
193+
}
194+
if (d != 0) {
195+
rb_ary_push(ccols, DBL2NUM(d + cvij));
196+
}
197+
else {
198+
rb_ary_push(ccols, INT2NUM(cvij));
199+
}
200+
}
201+
rb_ary_push(crows, ccols);
202+
}
203+
return crows;
204+
}
205+
else
206+
rows = (0 .. row_size - 1).collect {
207+
|i|
208+
(0 .. m.column_size - 1).collect {
209+
|j|
210+
vij = 0
211+
0.upto(column_size - 1) do
212+
|k|
213+
vij += self[i, k] * m[k, j]
214+
end
215+
vij
216+
}
217+
}
218+
end
219+
return Matrix.rows(rows, false)
220+
else
221+
x, y = m.coerce(self)
222+
return x * y
223+
end
224+
end
225+
end
144226

227+
def bench3
228+
max = 1000
229+
m1 = Matrix[*Array.new(max){|i|
230+
Array.new(max){|j|
231+
i + j
232+
}
233+
}]
234+
bm 'matrix' do
235+
(m1 * m1)
236+
end
237+
end
238+
239+
bench3()
240+
241+
__END__
242+
243+
bench1()
244+
bench2()
145245
__END__
146246
###
147247

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy