[ create a new paste ] login | about

Link: http://codepad.org/jhbYxEgU    [ raw code | fork ]

D, pasted on Mar 4:
// Compile with: dmd -O -release -inline -noboundscheck

import std.stdio, std.conv, std.array;

enum size_t SIZE = 2_000;

int[][] makeMatrix(bool doInit=true)(in size_t nRows, in size_t nCols) pure nothrow {
    auto mat = uninitializedArray!(typeof(return))(nRows, nCols);
    int count = 1;

    static if (doInit) {
        foreach (row; mat)
            foreach (ref x; row)
                x = count++;
    }

    return mat;
}

void matrixMult(in int[][] m1, in int[][] m2, int[][] m3) pure nothrow {
    foreach (immutable i, m1i; m1) {
        foreach (immutable j, ref m3ij; m3[i]) {
            int val = 0;
            foreach (immutable k, m2k; m2)
                val += m1i[k] * m2k[j];
            m3ij = val;
        }
    }
}

void main(in string[] args) {
    immutable size_t n = (args.length > 1) ? args[1].to!size_t : 1;

    immutable m1 = makeMatrix(SIZE, SIZE);
    immutable m2 = makeMatrix(SIZE, SIZE);
    auto mm = makeMatrix!false(SIZE, SIZE);

    foreach (immutable i; 0 .. n)
        matrixMult(m1, m2, mm);

    writefln("%d %d %d %d",
             mm[0][0], mm[2][3], mm[3][2], mm[4][4]);
}


Create a new paste based on this one


Comments: