1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94:
| program Project1;
{$APPTYPE CONSOLE}
uses SysUtils, Windows;
const DiffCount: array[0..255] of Byte = (0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3, 3,4,2,3,3,4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4, 5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4, 3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,4,2,3,3, 4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4, 4,5,4,5,5,6,4,5,5,6,5,6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4, 5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 );
function FastDistance(A, B: PDWord): Integer; inline; begin Result := 0; end;
function Distance(A, B: PDWord): Integer; inline; var AByte, BByte: PByte; begin AByte := PByte(A); BByte := PByte(B);
Result := DiffCount[AByte^ xor BByte^]; inc(AByte); inc(BByte); inc(Result, DiffCount[AByte^ xor BByte^]); inc(AByte); inc(BByte); inc(Result, DiffCount[AByte^ xor BByte^]); inc(AByte); inc(BByte); inc(Result, DiffCount[AByte^ xor BByte^]); end;
var A, B: DWord; I: Integer; TimeA, TimeB, Elapsed1, Elapsed2: Int64;
begin randomize; for I := 0 to 99 do begin A := Round(Random * High(DWord)); B := Round(Random * High(DWord)); if Distance(@A, @B) <> FastDistance(@A, @B) then begin Writeln('Test failed.'); break; end; end;
SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS); try A := Round(Random * High(DWord)); B := Round(Random * High(DWord)); QueryPerformanceCounter(TimeA); for I := 0 to 9999 do Distance(@A, @B); QueryPerformanceCounter(TimeB); Elapsed1 := TimeB-TimeA; Writeln('Original: ',Elapsed1); QueryPerformanceCounter(TimeA); for I := 0 to 9999 do FastDistance(@A, @B); QueryPerformanceCounter(TimeB); Elapsed2 := TimeB-TimeA; Writeln('New: ',Elapsed2); Writeln(Format('Result: %.2f x faster',[Elapsed1/Elapsed2])); finally SetPriorityClass(GetCurrentProcess, NORMAL_PRIORITY_CLASS); end;
readln; end. |