[fpc-pascal]Performance: Free Pascal vs GNU Pascal

Florian Klaempfl Florian.Klaempfl at gmx.de
Sun Dec 29 17:08:57 CET 2002


Mark Emerson wrote:
> Look at
> http://dada.perl.it/shootout/
> 
> 
> Thank you for this VALUABLE reference.  Let me ask (if I may)...on
> matrix multiplication, which appears to be a pretty clean, simple
> algorithm, why do you suppose Microsoft C (vc, and also vc++) is more
> than three times faster than FPC?

Because it's unfair :( :

Delphi test code:

-------------------------------

program matrix;


const
   SIZE = 30;

type TMatrix = array[0..SIZE-1, 0..SIZE-1] of integer;

procedure mkmatrix( rows, cols : integer; var mx : TMatrix);
var
   R, C, count : integer;
begin
   Dec(rows); Dec(cols);
   count:=1;
   for R:=0 to rows do
     for C:=0 to cols do begin
       mx[R, C] := count;
       Inc(count);
     end;
end;

procedure mmult(rows, cols: integer; const m1, m2: TMatrix; var mm: TMatrix);

[...]

var NUM, code, i: integer;
     M1, M2, MM: TMatrix;
begin
   NUM:=1;
   if ParamCount=1 then Val(ParamStr(1),NUM,code);

   mkmatrix(SIZE, SIZE, M1);
   mkmatrix(SIZE, SIZE, M2);
   for i:=0 to NUM do
     mmult(size, size, M1, M2, MM);
   WriteLn( MM[0, 0],' ',MM[2, 3],' ',MM[3, 2],' ',MM[4, 4]);
end.

-------------------------------

FPC test code:

program matrix;
uses SysUtils;

const
     size = 30;

type tMatrix = array[0..size, 0..size] of longint;

procedure mkmatrix( rows, cols : integer; var mx : tMatrix);
var
     R, C : integer;
     count : longint;
begin
     Dec(rows);
     Dec(cols);
     count := 1;
     for R := 0 to rows do
     begin
         for C := 0 to cols do
         begin
             mx[R, C] := count;
             Inc(count);
         end;
     end;
End;

procedure mmult(rows, cols : integer; m1, m2 : tMatrix; var mm : tMatrix );

[...]

var NUM, I : integer;
     M1, M2, MM : tMatrix;

begin
     if ParamCount = 0 then
         NUM := 1
     else
         NUM := StrToInt(ParamStr(1));

     if NUM < 1 then NUM := 1;

     mkmatrix(size, size, M1);
     mkmatrix(size, size, M2);

     for I := 0 To NUM do
     begin
         mmult(size, size, M1, M2, MM);
     end;
     WriteLn( IntToStr(MM[0, 0]) + ' ' + IntToStr(MM[2, 3]) + ' ' +
              IntToStr(MM[3, 2]) + ' ' + IntToStr(MM[4, 4]));
end.

-------------------------------

Note the matrix sizes and the calling conventions: passing by const is much 
faster than parameter passing by value. Further, test with float, integer 
matrix operations are very unlikely :)





More information about the fpc-pascal mailing list