program test;
{$mode objfpc}
{$optimization on}
uses SysUtils;
type
TMyArray = array of NativeInt;
TBenchProc = procedure (const arr: TMyArray; step: NativeInt);
const
step_over = 10;
max_step = 128*1024 + step_over;
steps_count = 16*1024*1024;
size = 2*1024*1024*1024 div sizeof(TMyArray[0]);
procedure bench_read(const arr: TMyArray; step: NativeInt);
var
i, dummy: NativeInt;
c: NativeInt = 0;
timer: TDateTime;
begin
if step>max_step then Halt(1);
timer:=Now;
while c < steps_count do
begin
if (size div step)>(steps_count-c) then
i:=size - (steps_count-c)*step
else
i:=0;
while (i < size) do
begin
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
dummy := arr[i]; inc(i, step);
inc(c, 16);
end;
end;
WriteLn('step: ', step: 8, ', time: ', (Now-timer)*MSecsPerDay:0:0, ' ms');
end;
procedure bench_write(const arr: TMyArray; step: NativeInt);
var
i: NativeInt;
c: NativeInt = 0;
timer: TDateTime;
zero: NativeInt = 0;
begin
if step>max_step then Halt(1);
timer:=Now;
while c < steps_count do
begin
if (size div step)>(steps_count-c) then
i:=size - (steps_count-c)*step
else
i:=0;
while (i < size) do
begin
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
arr[i]:=zero; inc(i, step);
inc(c, 16);
end;
end;
WriteLn('step: ', step: 8, ', time: ', (Now-timer)*MSecsPerDay:0:0, ' ms');
end;
procedure bench_readwrite(const arr: TMyArray; step: NativeInt);
var
i: NativeInt;
c: NativeInt = 0;
timer: TDateTime;
zero: NativeInt = 0;
begin
if step>max_step then Halt(1);
timer:=Now;
while c < steps_count do
begin
if (size div step)>(steps_count-c) then
i:=size - (steps_count-c)*step
else
i:=0;
while (i < size) do
begin
arr[i]:=zero; inc(i, step);
zero:=arr[i]; inc(i, step);
arr[i]:=zero; inc(i, step);
zero:=arr[i]; inc(i, step);
arr[i]:=zero; inc(i, step);
zero:=arr[i]; inc(i, step);
arr[i]:=zero; inc(i, step);
zero:=arr[i]; inc(i, step);
arr[i]:=zero; inc(i, step);
zero:=arr[i]; inc(i, step);
arr[i]:=zero; inc(i, step);
zero:=arr[i]; inc(i, step);
arr[i]:=zero; inc(i, step);
zero:=arr[i]; inc(i, step);
arr[i]:=zero; inc(i, step);
zero:=arr[i]; inc(i, step);
inc(c, 16);
end;
end;
WriteLn('step: ', step: 8, ', time: ', (Now-timer)*MSecsPerDay:0:0, ' ms');
end;
var
bench_procs: array of TBenchProc = (@bench_read, @bench_write, @bench_readwrite);
steps: array of integer = (1,2,3,4,5,6,7,8,9,10,15,16,17,23,24,25,30,31,32,33,34,47,48,49,62,63,64,65,66,95,96,97,126,127,128,129,
254,255,256,257,510,511,512,513,1022,1023,1024,1025,1026,2046,2047,2048,2049,
16382,16383,16384,16385,16386,
32766,32767,32768,32769,32770,
65534,65535,65536,65537,65538,
131070,131071,131072,131073,131074
);
arr: TMyArray;
st: NativeInt;
bench: TBenchProc;
begin
SetLength(arr, size + max_step*16);
WriteLn('steps count: ', steps_count);
for bench in bench_procs do
for st in steps do
bench(arr, st);
ReadLn;
end.