static arrays.

Dave Dave_member at pathlink.com
Wed Jan 31 18:23:10 PST 2007


torhu wrote:
> Dave wrote:
>> Whoops - I should have made it clear that the programs perform roughly 
>> the same (D being about 10% faster), but only if the static matrices 
>> are passed byref (using pointer params) in D.
>>
> 
> I'm not sure what you mean.  Static arrays in D are passed as the 
> address of the first element.  They are compatible with C arrays, at 
> least in this respect.  So the speedup has to be caused by something else.

Thanks - you're right. I think I made a bad assumption based on a botched comparison.

Looking at it again, the following performs just as well. When I 'ported' it the first time from 
Delphi Pascal, I most likely had a typo in the original D code (?)

module matrix2;

import std.conv, std.stdio;

void main(char[][] args)
{
   int NUM = args.length > 1 ? toInt(args[1]) : 1;
   TMatrix M1, M2, MM;

   mkmatrix(SIZE, SIZE, M1);
   mkmatrix(SIZE, SIZE, M2);
   for(int i = 0; i < NUM; i++)
     mmult(SIZE, SIZE, M1, M2, MM);
   writefln(MM[0][0]," ",MM[2][3]," ",MM[3][2]," ",MM[4][4]);
}

const SIZE = 30;

typedef int[SIZE][SIZE] TMatrix;

void mkmatrix(int rows, int cols, TMatrix mx)
{
   int count = 1;
   for(int R = 0; R < rows; R++)
     for(int C = 0; C < cols; C++)
     {
       mx[R][C] = count;
       count++;
     }
}

void mmult(int rows, int cols, TMatrix m1, TMatrix m2, TMatrix mm)
{
   for(int i = 0; i < rows; i++)
     for(int j = 0; j < cols; j++)
     {
       int val = 0;
       for(int k = 0; k < cols; k++)
         val += m1[i][k] * m2[k][j];
       mm[i][j] = val;
     }
}



More information about the Digitalmars-d mailing list