内存比较函数 - CompareMem(文件、数组等比较)

8 篇文章 0 订阅
8 篇文章 0 订阅
所属单元:SysUtils

函数功能:快速内存比较函数,可对两个文件比较、数组进行比较。据测试速度比一般算法要高60~110%。


一、文件的比较
你是否遇到过比较两个任意后缀名文件内容是否相同的问题?(TFileStream其他类可以方便的操作这些文件,但如果说到比对内容,恐怕……)

下面就用这个函数快速而简单的比对两个文件内容的是否相同。

procedure TForm1.btn1Click(Sender: TObject);
var
cs1, cs2:TMemorystream;
begin
cs1 := TMemoryStream.Create;
cs2 := TMemoryStream.Create;
try
    cs1.LoadFromFile('C:\a.bin');
    cs2.LoadFromFile('C:\b.bin');
    if (cs1.Size = cs2.Size) and CompareMem(cs1.Memory, cs2.Memory,cs1.Size) then
      ShowMessage('文件内容相同')
    else
      ShowMessage('文件内容不同!');
finally
    cs1.free;
    cs2.free;
end;
end;

二、比较两个二维数组是否相等(转)
{对比静态数组}
procedure TForm1.btn4Click(Sender: TObject);
var
arr1: array[0..3] of AnsiChar;
arr2: array[0..3] of Byte;
begin
arr1[0] := 'A';
arr1[1] := 'B';
arr1[2] := 'C';
arr1[3] := 'D';
arr2[0] := 65;
arr2[1] := 66;
arr2[2] := 67;
arr2[3] := 68;

if CompareMem(@arr1, @arr2, SizeOf(arr1)) then
    ShowMessage('arr1 与 arr2 中的数据相同');
end;

{对比动态数组}
procedure TForm1.btn2Click(Sender: TObject);
var
arr1: array of AnsiChar;
arr2: array of Byte;
begin
SetLength(arr1, 4);
SetLength(arr2, 4);
arr1[0] := 'A';
arr1[1] := 'B';
arr1[2] := 'C';
arr1[3] := 'D';
arr2[0] := 65;
arr2[1] := 66;
arr2[2] := 67;


arr2[3] := 68;
if CompareMem(arr1, arr2, SizeOf(arr1)) then
    ShowMessage('arr1 与 arr2 中的数据相同');
end;

{多维数组也一样}

procedure TForm1.btn3Click(Sender: TObject);
var
arr1: array[0..1, 0..1] of AnsiChar;
arr2: array[0..1, 0..1] of Byte;
begin
arr1[0, 0] := 'A';
arr1[0, 1] := 'B';
arr1[1, 0] := 'C';
arr1[1, 1] := 'D';
arr2[0, 0] := 65;
arr2[0, 1] := 66;
arr2[1, 0] := 67;
arr2[1, 1] := 68;

if CompareMem(@arr1, @arr2, SizeOf(arr1)) then
    ShowMessage('arr1 与 arr2 中的数据相同');
end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮我看下这个方法怎么使用的27 /** 28 Compares the contents of two buffers. 29 30 This function compares Length bytes of SourceBuffer to Length bytes of DestinationBuffer. 31 If all Length bytes of the two buffers are identical, then 0 is returned. Otherwise, the 32 value returned is the first mismatched byte in SourceBuffer subtracted from the first 33 mismatched byte in DestinationBuffer. 34 35 If Length > 0 and DestinationBuffer is NULL, then ASSERT(). 36 If Length > 0 and SourceBuffer is NULL, then ASSERT(). 37 If Length is greater than (MAX_ADDRESS - DestinationBuffer + 1), then ASSERT(). 38 If Length is greater than (MAX_ADDRESS - SourceBuffer + 1), then ASSERT(). 39 40 @param DestinationBuffer A pointer to the destination buffer to compare. 41 @param SourceBuffer A pointer to the source buffer to compare. 42 @param Length The number of bytes to compare. 43 44 @return 0 All Length bytes of the two buffers are identical. 45 @retval Non-zero The first mismatched byte in SourceBuffer subtracted from the first 46 mismatched byte in DestinationBuffer. 47 48 **/ 49 INTN 50 EFIAPI 51 CompareMem ( 52 IN CONST VOID *DestinationBuffer, 53 IN CONST VOID *SourceBuffer, 54 IN UINTN Length 55 ) 56 { 57 if (Length == 0 || DestinationBuffer == SourceBuffer) { 58 return 0; 59 } 60 ASSERT (DestinationBuffer != NULL); 61 ASSERT (SourceBuffer != NULL); 62 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)DestinationBuffer)); 63 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)SourceBuffer)); 64 65 return InternalMemCompareMem (DestinationBuffer, SourceBuffer, Length); 66 }
07-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值