Hi Rob

The compiler is complaining about the const Zeros declaration
   Zeros: array[0..1023] of Byte = [0];

Incompatible types: 'Array' and 'Set'.

Should it perhaps need to be a global variable which I fill using 
FillChar?

Regards,
Ross.

----- Original Message ----- 
From: "Rob Kennedy" <[EMAIL PROTECTED]>
To: "Borland's Delphi Discussion List" <[email protected]>
Sent: Friday, November 04, 2005 6:08 AM
Subject: Re: Test memory block


Ross Levis wrote:
> I'm looking for the fastest way to test if a block of memory is all
> zeros or not.  This needs to be as fast as possible since it is being
> tested many times a second.

You can use the CompareMem function to compare large blocks of memory at
once. It will usually compare at least four bytes at a time instead of
comparing each byte separately. Just have a static buffer of all zeros
and compare your given buffer to that one. For example:

const
   Zeros: array[0..1023] of Byte = [0];

function IsZero(const Buffer: Pointer; const BufferSize: Cardinal): 
Boolean;
// Returns True if the buffer is all-bits-zero. Returns False if
// the buffer contains non-zero bytes, or if its length is zero.
var
   BytesRemaining: Cardinal;
   ZeroBytes: Cardinal;
   Buf: PByte;
begin
   Result := False;
   Buf := Buffer;
   ZeroBytes := SizeOf(Zeros);
   BytesRemaining := BufferSize;
   while BytesRemaining >= ZeroBytes do begin
     if not CompareMem(@Zeros, Buf, ZeroBytes) then exit;
     Dec(BytesRemaining, ZeroBytes);
     Inc(Buf, ZeroBytes);
   end;
   if BytesRemaining > 0 then
     Result := CompareMem(@Zeros, Buf, BytesRemaining);
end;

This function should work more efficiently the larger you make Zeros.
You can also make CompareMem faster by using an implementation from the
Fastcode Project.

http://fastcode.sourceforge.net/fastcodeproject/60.htm

Download form here:

http://fastcode.sourceforge.net/fastcodeproject/55.htm

-- 
Rob 

_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to