program PointerIteration;
{$APPTYPE CONSOLE}
uses
System.SysUtils, System.Diagnostics;
function GetHashCode1(Data: PByte; Length: Integer): Integer;
var
i: Integer;
begin
Result := 17;
for i := 1 to Length do
begin
Result := Result*23 + Data^;
inc(Data);
end;
end;
function GetHashCode2(Data: PByte; Length: Integer): Integer;
var
EndOfMemoryBlock: PByte;
begin
EndOfMemoryBlock := PByte(NativeInt(Data)+Length);
Result := 17;
while Data<EndOfMemoryBlock do
begin
Result := Result*23 + Data^;
inc(Data);
end;
end;
function GetHashCode3(Data: PByte; Length: Integer): Integer;
type
TByteArray = array [0..0] of Byte;
var
i: Integer;
ByteArray: ^TByteArray absolute Data;
begin
Result := 17;
for i := 0 to Length-1 do
Result := Result*23 + ByteArray^[i];
end;
var
i: Integer;
Data: array of Byte;
Stopwatch: TStopwatch;
begin
SetLength(Data, 512*1024*1024);
for i := 0 to high(Data) do
Data[i] := i mod 256;
Stopwatch := TStopwatch.StartNew;
for i := 1 to 10 do
GetHashCode1(PByte(Data), Length(Data));
Writeln(Stopwatch.ElapsedMilliseconds);
Stopwatch := TStopwatch.StartNew;
for i := 1 to 10 do
GetHashCode2(PByte(Data), Length(Data));
Writeln(Stopwatch.ElapsedMilliseconds);
Stopwatch := TStopwatch.StartNew;
for i := 1 to 10 do
GetHashCode3(PByte(Data), Length(Data));
Writeln(Stopwatch.ElapsedMilliseconds);
Readln;
end.