In my work, I use frequently big matrix multiplications, so its efficiency is vital. In the following simple code, I do a multiplication of two 1000*1000 square matrices, which takes about 11 seconds on my mac book pro with a 2.7 GHz dual core i7 cpu. In comparison, the same matrix multiplication takes only 0.07 second by a simple Mathematica code (I use the version 8.0.4) on the same machine. What's wrong of fpc? And any suggestions? BTW, in both cases, the platform I use is Windows 8.1. Sorry for my poor English.
program matmultiply;
Uses SysUtils;
Const
n=1000;
Type
Vector=Array[0..n-1] of Extended;
Matrix=Array[0..n-1] of Vector;
Var
StartMS:double;
d,dd:Matrix;
Function Multiply(Const mat1,mat2:Matrix):Matrix;
Var
i,j,k:Word;
sum:Extended;
v:Vector;
Begin
For i:=0 to n-1 do
For j:=0 to n-1 do
Begin
sum:=0;v:=mat1[i];
For k:=0 to n-1 do sum+=v[k]*mat2[k,j];
Multiply[i,j]:=sum;
End;
End;
Function ChebyPts:Vector;
Var
i:Word;
Begin
For i:=0 to n-1 do ChebyPts[i]:=Cos(i*Pi/(n-1));
End;
Function ChebyDiff:Matrix;
Var
i,j:Word;
c,CP:Vector;
Begin
For i:=1 to n-2 do c[i]:=1;
c[0]:=2;c[n-1]:=2;
CP:=ChebyPts;
For i:=0 to n-1 do
For j:=0 to n-1 do
if i<>j then ChebyDiff[i,j]:=(c[i]/c[j])*Cos((i+j)*Pi)/(CP[i]-CP[j])
else if i=0 then ChebyDiff[i,j]:=(2*(n-1)*(n-1)+1)/6
else if i=n-1 then ChebyDiff[i,j]:=-(2*(n-1)*(n-1)+1)/6
else ChebyDiff[i,j]:=-0.5*CP[j]/(1-CP[j]*CP[j]);
End;
begin
d:=ChebyDiff;
StartMS:=timestamptomsecs(datetimetotimestamp(now));
dd:=Multiply(d,d);
Writeln('Serially processed in '+floattostr(timestamptomsecs(datetimetotimestamp(now))-StartMS)+'ms.');
end.