The random generator is in both cases the Mersenne Twister (routine mt19937
from the authors' site). And I had forgot the Fortran program.
Anyway you are right: the pseudorandom generator takes the most time in the
program. But here is another example, this time both in Pascal and in
Fortran:
------------------------------------------------
program matrix;
uses GPC, SysUtils;
const
size = 30;
type tMatrix = array[0..size, 0..size] of longint;
VAR
time1, time2 : longCard;
dummy,
j, big, cpu1, cpu2 : integer;
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
i, j, k : integer;
val: longint;
begin
Dec(rows);
Dec(cols);
For i := 0 To rows do
begin
For j := 0 To cols do
begin
val := 0;
For k := 0 To cols do
begin
Inc(val, m1[i, k] * m2[k, j]);
end;
mm[i, j] := val;
end;
end;
End;
var NUM, I : integer;
M1, M2, MM : tMatrix;
begin
writeln ('# MATRIX MULTIPLICATION: GPC');
time1 := GetMicroSecondTime;
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]));
dummy := 0;
time2 := GetMicroSecondTime;
writeln('GPC: elapsed time: ', ( (time2-time1) * 0.000001):7:2, ' seconds, '
);
end.
------------------------------------------------
program perfor2for
integer i
character*20 argum
integer size
parameter ( size = 30 )
integer m1 ( 0:size, 0:size), m2 ( 0:size, 0:size),
& mm ( 0:size, 0:size), mx ( 0:size, 0:size )
real etime
real elapsed(2)
real total
write (*,*) '# MATRIX MULTIPLICATION: IFORT'
call getarg ( 1, argum )
read ( argum, * ) num
write (*,*) '# size = ', size
if ( num .le. 1 ) num =1
call mkmatrix ( size, size, m1 )
call mkmatrix ( size, size, m2 )
write (*,*)'------------------------------------------------------'
write (*,*) m1 (0,0), ' ', m1(2,3), ' ', m1(3,2), ' ', m1(4,4)
write (*,*) m2 (0,0), ' ', m2(2,3), ' ', m2(3,2), ' ', m2(4,4)
write (*,*)'------------------------------------------------------'
do i = 1, num
call mmult ( size, size, m1, m2, mm )
enddo
write (*,*) mm (0,0), ' ', mm(2,3), ' ', mm(3,2), ' ', mm(4,4)
total = etime ( elapsed )
print *, 'Elapsed time =', total, ' secondi '! , ' user=', elapsed(1), '
system=', elapsed(2)
stop
end
subroutine mkmatrix ( rows, cols, mx );
integer r, c, rows, cols
include 'size.h'
integer mx ( 0:size, 0:size ), count
rows = rows - 1
cols = cols - 1
count = 1
write (*,*) '# rows = ', rows, ' cols = ', cols
do r = 0, rows
do c = 0, cols
mx ( r, c ) = count
count = count + 1
enddo
enddo
return
end
subroutine mmult ( rows, cols, m1, m2, mm )
integer r, c, rows, cols, i, j, k
include 'size.h'
integer m1( 0:size, 0:size ), m2( 0:size, 0:size ), mm ( 0:size, 0:size ),
count, val
rows = rows - 1
cols = cols - 1
do i = 0, rows
do j = 0, cols
val = 0
do k = 0, cols
val = val + m1(i,k)*m2(k, j)
enddo
mm(i, j) = val
enddo
enddo
return
end
------------------------------------------------------------------
here the difference is marked. Perhaps by exhanging columns with rows one can
make Pascal execution faster. I will try.
Thanks, best regards
Silvio a Beccara