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