Oh gosh,  I can't resist.

Odds are the compiler is smart enough to recognize that (LengthInBytes mod
4) is the same as (LengthInBytes AND 3) which is faster but if not, much
better to use the AND.

Without looking at the compiled code the terminaton condition of the for
loop may or may not be executed every time.  I would hope not since it
doesn't change within the loop.  Once again,  a (LengthInBytes shr 2)-1 is
less expensive in processor time since the CPU has a barrel shifter.  i.e.
It's a one clock cycle shift.  Although the load of the shift count would
also take an instruction.  So in this case,  two sequential SHR machine
instructions would take the same time as a load shift count and do an n bit
shift.

But for this to be a real competition,  the compiled code has to be posted.
I tried and the list server stripped it so mine went as a private email to
Ross.

John


> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Malcolm Clark
> Sent: Saturday, November 05, 2005 4:07 AM
> To: Borland's Delphi Discussion List
> Subject: Re: Test memory block
> 
> 
>  >Just as a matter of interest, I came up with my own fast 
> method working
>  >along the lines of John's method but I think this is tidier.
> 
> This is starting to resemble a coding competition! Here's my 
> suggestion:
> 
> function IsZero(const AddrOfDataToSearch:Pointer; const 
> LengthInBytes:LongWord) : boolean;
> var
>   p:pLongWord; //pLongWord defined System unit in D7
>   i:LongWord;
> begin
>   p:=pLongWord(AddrOfDataToSearch);
>   result:=false;
>   for i:=0 to ((LengthInBytes div 4)-1) do
>     if  p^ <> 0 then
>       exit
>     else
>       inc(p);
>   case LengthInBytes mod 4 of
>     0: result:=true;
>     1: if pByte(p)^=0 then result:=true;
>     2: if pWord(p)^=0 then result:=true;
>     3: if ((pWord(p)^=0) and (PByte(LongWord(p)+2)^=0)) then 
> result:=true;
>   end;
> end;//IsZero
> _______________________________________________
> Delphi mailing list -> [email protected]
> http://www.elists.org/mailman/listinfo/delphi
> 
> 

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

Reply via email to