program CompareFileBench;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Classes,
System.Hash,
System.Diagnostics;
function AreFilesEqual(
const FileNameA, FileNameB:
string; BlockSize: Integer = 4096): Boolean;
var
a: TBytes;
b: TBytes;
cntA: Integer;
cntB: Integer;
readerA: TStream;
readerB: TStream;
begin
Result := False;
readerA := TFileStream.Create(FileNameA, fmOpenRead);
try
readerB := TFileStream.Create(FileNameB, fmOpenRead);
try
SetLength(a, BlockSize);
SetLength(b, BlockSize);
repeat
cntA := readerA.
Read(a, BlockSize);
cntB := readerB.
Read(b, BlockSize);
if cntA <> cntB
then Exit;
if cntA = 0
then Break;
if not CompareMem(@a[0], @b[0], cntA)
then Exit;
until cntA < BlockSize;
Result := True;
finally
readerB.Free;
end;
finally
readerA.Free;
end;
end;
procedure Test;
var
c1:
string;
c2:
string;
I: Integer;
sw: TStopwatch;
begin
c1 := '
<some file>';
c2 := '
<some other file with the same content>';
{ Dummy call to fill the cache }
AreFilesEqual(c1, c2, 16*1024);
sw := TStopwatch.StartNew;
for I := 1
to 1000
do
AreFilesEqual(c1, c2, 16*1024);
Writeln(sw.ElapsedMilliseconds);
{ Dummy call to fill the cache }
THashMD5.GetHashBytesFromFile(c1);
THashMD5.GetHashBytesFromFile(c2);
sw := TStopwatch.StartNew;
for I := 1
to 1000
do begin
THashMD5.GetHashBytesFromFile(c1);
THashMD5.GetHashBytesFromFile(c2);
end;
Writeln(sw.ElapsedMilliseconds);
end;
begin
try
Test;
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
Readln;
end.