Want more features on Pastebin? Sign Up, it's FREE!
Guest

SO#13997397

By: a guest on Dec 22nd, 2012  |  syntax: Delphi  |  size: 1.47 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. program PointerIteration;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   System.SysUtils, System.Diagnostics;
  7.  
  8. function GetHashCode1(Data: PByte; Length: Integer): Integer;
  9. var
  10.   i: Integer;
  11. begin
  12.   Result := 17;
  13.   for i := 1 to Length do
  14.   begin
  15.     Result := Result*23 + Data^;
  16.     inc(Data);
  17.   end;
  18. end;
  19.  
  20. function GetHashCode2(Data: PByte; Length: Integer): Integer;
  21. var
  22.   EndOfMemoryBlock: PByte;
  23. begin
  24.   EndOfMemoryBlock := PByte(NativeInt(Data)+Length);
  25.   Result := 17;
  26.   while Data<EndOfMemoryBlock do
  27.   begin
  28.     Result := Result*23 + Data^;
  29.     inc(Data);
  30.   end;
  31. end;
  32.  
  33. function GetHashCode3(Data: PByte; Length: Integer): Integer;
  34. type
  35.   TByteArray = array [0..0] of Byte;
  36. var
  37.   i: Integer;
  38.   ByteArray: ^TByteArray absolute Data;
  39. begin
  40.   Result := 17;
  41.   for i := 0 to Length-1 do
  42.     Result := Result*23 + ByteArray^[i];
  43. end;
  44.  
  45. var
  46.   i: Integer;
  47.   Data: array of Byte;
  48.   Stopwatch: TStopwatch;
  49.  
  50. begin
  51.   SetLength(Data, 512*1024*1024);
  52.   for i := 0 to high(Data) do
  53.     Data[i] := i mod 256;
  54.  
  55.   Stopwatch := TStopwatch.StartNew;
  56.   for i := 1 to 10 do
  57.     GetHashCode1(PByte(Data), Length(Data));
  58.   Writeln(Stopwatch.ElapsedMilliseconds);
  59.  
  60.   Stopwatch := TStopwatch.StartNew;
  61.   for i := 1 to 10 do
  62.     GetHashCode2(PByte(Data), Length(Data));
  63.   Writeln(Stopwatch.ElapsedMilliseconds);
  64.  
  65.   Stopwatch := TStopwatch.StartNew;
  66.   for i := 1 to 10 do
  67.     GetHashCode3(PByte(Data), Length(Data));
  68.   Writeln(Stopwatch.ElapsedMilliseconds);
  69.  
  70.   Readln;
  71. end.
clone this paste RAW Paste Data