Fast multidimensional Arrays
Daniel Kozak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 29 07:11:01 PDT 2016
Dne 29.8.2016 v 15:57 rikki cattermole via Digitalmars-d-learn napsal(a):
> My bad, fixed:
>
> double[1000][] A, B, C;
>
> void main() {
> A = new double[1000][1000];
> B = new double[1000][1000];
> C = new double[1000][1000];
>
> import std.conv : to;
> import std.datetime;
> import std.stdio : writeln;
>
> ini(A);
> ini(B);
> ini(C);
>
> auto r = benchmark!run_test(10000);
> auto res = to!Duration(r[0]);
> writeln(res);
> }
>
> void run_test() {
> MatMul(A, B, C);
> }
>
> void ini(T)(T mtx) {
> foreach(ref v; mtx) {
> v = 3.4;
> }
>
> foreach(i, v; mtx) {
> foreach(j, ref vv; v) {
> vv += (i * j) + (0.6 * j);
> }
> }
> }
>
> void MatMul(T)(T A, T B, T C) {
> foreach(cv; C) {
> cv = 0f;
> }
>
> foreach(i, cv; C) {
> foreach(j, av; A[i]) {
> auto bv = B[j];
> foreach(k, cvv; cv) {
> cvv += av * bv[k];
> }
> }
> }
>
> }
This will not work, you need to add some ref :).
foreach(i, ref cv; C) {
foreach(j, av; A[i]) {
auto bv = B[j];
foreach(k, ref cvv; cv) {
cvv += av * bv[k];
}
}
}
More information about the Digitalmars-d-learn
mailing list