How to tune numerical D? (matrix multiplication is faster in g++ vs gdc)
Walter Bright
newshound2 at digitalmars.com
Mon Mar 4 09:11:53 PST 2013
On 3/3/2013 7:48 PM, J wrote:
> void mmult(int[][] m1, int[][] m2, int[][] m3)
> {
> foreach(int i, int[] m1i; m1)
> {
> foreach(int j, ref int m3ij; m3[i])
> {
> int val;
> foreach(int k, int[] m2k; m2)
> {
> val += m1i[k] * m2k[j];
> }
> m3ij = val;
> }
> }
> }
[...]
> ////// C++ version
> int **mmult(int rows, int cols, int **m1, int **m2, int **m3) {
> int i, j, k, val;
> for (i=0; i<rows; i++) {
> for (j=0; j<cols; j++) {
> val = 0;
> for (k=0; k<cols; k++) {
> val += m1[i][k] * m2[k][j];
> }
> m3[i][j] = val;
> }
> }
> return(m3);
> }
One difference that jumps out at me is you have extra variables and ref types in
the D version, and in the C++ version you have "cached" the row & column loop
limits. (I.e. the C++ version assumes a rectangular matrix, while the D one has
a (presumably) different length for each column.)
More information about the Digitalmars-d
mailing list