program testarray;
uses
sysutils;
type
aos=array of single;
var
arr0, arr1, arr2, arr3 : array of single;
ratio : single = 0.123;
ratio1 : single = 0.345;
ratio2 : single = 0.567;
ratio3 : single = 0.890;
procedure CalculArrays() ; // add and multiply
var
i : int64;
begin
// all arrays have the same size
for i := 0 to length(arr0) -1 do
begin
arr0[i]:=0;
arr0[i] := ratio * ( (ratio1 * arr1[i]) + (ratio2 * arr2[i]) + (ratio3 * arr3[i]) ); // here ratio multiply
end;
end;
procedure CalculArrays2(var arr0:aos;arr1:aos;arr2:aos;arr3:aos); // add and multiply
var
i : int64;
begin
// all arrays have the same size
for i := 0 to length(arr0) -1 do
begin
arr0[i]:=0;
arr0[i] := ratio * ( (ratio1 * arr1[i]) + (ratio2 * arr2[i]) + (ratio3 * arr3[i]) ); // here ratio multiply
end;
end;
procedure CalculArrays3(const arr0,arr1,arr2,arr3:aos); //
var
i : int64;
begin
// all arrays have the same size
for i := 0 to length(arr0) -1 do
begin
arr0[i]:=0;
arr0[i] := ratio * ( (ratio1 * arr1[i]) + (ratio2 * arr2[i]) + (ratio3 * arr3[i]) ); // here ratio multiply
end;
end;
procedure CalculArrays4(arr0,arr1,arr2,arr3:aos); //
var
i : int64;
begin
// all arrays have the same size
for i := 0 to length(arr0) -1 do
begin
arr0[i]:=0;
arr0[i] := ratio * ( (ratio1 * arr1[i]) + (ratio2 * arr2[i]) + (ratio3 * arr3[i]) ); // here ratio multiply
end;
end;
var
lim:int64=200000000;
i,k,t,t2,totg,totp, totp2, totp3 :int64;
begin
writeln('Filling arrays, please wait a second or so . . .');
setlength(arr0,lim);
setlength(arr1,lim);
setlength(arr2,lim);
setlength(arr3,lim);
for i:=0 to lim-1 do
begin
arr1[i]:=i;
arr2[i]:=i;
arr3[i]:=i;
end;
totp:=0;
totp2:=0;
totp3:=0;
totg:=0;
writeln('OK arrays filled, START the race . . .');
for k:=1 to 5 do
begin
writeln('Pass ',k);
t:=gettickcount64;
CalculArrays();
t2:=gettickcount64-t;
totg:=totg+t2;
t:=gettickcount64;
CalculArrays2(arr0,arr1,arr2,arr3);
t2:=gettickcount64-t;
totp:=totp+t2;
t:=gettickcount64;
CalculArrays3(arr0,arr1,arr2,arr3);
t2:=gettickcount64-t;
totp2:=totp2+t2;
t:=gettickcount64;
CalculArrays4(arr0,arr1,arr2,arr3);
t2:=gettickcount64-t;
totp3:=totp3+t2;
end;
writeln('Time with globals ',totg);
writeln('Time with params var ',totp);
writeln('Time with params const ',totp2);
writeln('Time with params nil ',totp3);
writeln('Press return to end');
readln;
end.