Delphi

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Test memory block

    12 answers - 1493 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    This smells remarkably like a school assignment. Why would you want to do
    this sort of thing many times in real life since testing what touches your
    memory block is probably more efficient?
    John Dammeyer
    Wireless CAN with the CANRF module now available.
    Automation Artisans Inc.
    Ph. 1 250 544 4950
    Message
    From: delphi-bounces (AT) elists (DOT) org
    [mailto:delphi-bounces (AT) elists (DOT) org] Behalf Ross Levis
    Sent: Thursday, November 03, 2005 2:54 AM
    To: Delphi Discussion List
    Subject: Test memory block
    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.
    The memory block can be any length. Assuming Buffer is a
    pointer to a
    block of memory, and BufferSize is the length of the block, I'm
    currently doing it like this.
    type
    TBufferArray = array[00] of Byte;
    PBufferArray = ^TBufferArray;
    var
    BufArray: BufferArray;
    i: Integer;
    begin
    BufArray := Buffer;
    Result := False;
    for i := 0 to BufferSize-1 do
    if Buffer[i] <0 then Exit;
    Result := True;
    This is reasonably quick but I'm wondering if there is a
    faster way that
    does not need to increment an integer many times.
    Thanks,
    Ross.
    Delphi mailing list -Delphi (AT) elists (DOT) org
    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.1 | | 1061 bytes | |

    No, it's a real-life commercial audio player application. At the end of
    playing a song, I need to flush the remaining audio buffers from inside
    a DLL. I send it a zeroed buffer and it returns the remaining audio in
    the buffer, and the only way to know it's finished is by testing the
    returned buffer is all zero.

    Generally the buffer will be between 2k and 8k.

    I'll give your CompareMem idea a go Rob and do some time tests. Many
    thanks for that.

    Ross.

    Message
    From: "John Dammeyer" <johnd (AT) autoartisans (DOT) com>
    To: "'Borland's Delphi Discussion List'" <delphi (AT) elists (DOT) org>
    Sent: Friday, November 04, 2005 5:07 AM
    Subject: RE: Test memory block

    This smells remarkably like a school assignment. Why would you want to
    do
    this sort of thing many times in real life since testing what touches
    your
    memory block is probably more efficient?

    John Dammeyer

    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.2 | | 903 bytes | |

    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.

    type
    TBuffer32 = array[00] of LongWord;
    PBuffer32 = ^TBuffer32;

    TBuffer8 = array[00] of Byte;
    PBuffer8 = ^TBuffer8;

    function IsZero(Buffer32: Pointer; BufferSize: Integer): Boolean;
    var
    Size32: Integer;
    Buffer8: PBuffer8;
    Buffer32: PBuffer32;
    i: Integer;
    begin
    Result := False;
    Buffer32 := Buffer;
    Size32 := BufferSize shr 2;
    for i := 0 to Size32-1 do
    if Buffer32[i] <0 then // test 4 bytes at a time
    Exit;
    Buffer8 := Buffer;
    for i := 0 to BufferSize - (Size32 shl 2) -1 do
    if Buffer8[i] <0 then // test remaining 1,2, or 3 bytes
    Exit;
    Result := True;
    end;

    Regards,
    Ross.

    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.3 | | 1177 bytes | |

    typo

    function IsZero(Buffer: Pointer;

    Message
    From: "Ross Levis" <ross (AT) stationplaylist (DOT) com>
    To: "Borland's Delphi Discussion List" <delphi (AT) elists (DOT) org>
    Sent: Saturday, November 05, 2005 12:09 PM
    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.

    type
    TBuffer32 = array[00] of LongWord;
    PBuffer32 = ^TBuffer32;

    TBuffer8 = array[00] of Byte;
    PBuffer8 = ^TBuffer8;

    function IsZero(Buffer32: Pointer; BufferSize: Integer): Boolean;
    var
    Size32: Integer;
    Buffer8: PBuffer8;
    Buffer32: PBuffer32;
    i: Integer;
    begin
    Result := False;
    Buffer32 := Buffer;
    Size32 := BufferSize shr 2;
    for i := 0 to Size32-1 do
    if Buffer32[i] <0 then // test 4 bytes at a time
    Exit;
    Buffer8 := Buffer;
    for i := 0 to BufferSize - (Size32 shl 2) -1 do
    if Buffer8[i] <0 then // test remaining 1,2, or 3 bytes
    Exit;
    Result := True;
    end;

    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.4 | | 898 bytes | |

    >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 ADataToSearch:Pointer; const
    LengthInBytes:LongWord) : boolean;
    var
    p:pLongWord; //pLongWord defined System unit in D7
    i:LongWord;
    begin
    p:=pLongWord(ADataToSearch);
    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 -Delphi (AT) elists (DOT) org
  • No.5 | | 1543 bytes | |

    That's very good Melcome, I may use your exact code. The extra
    0.000000001 millisecond saving at the end is excellent :-)

    It appears to be similar to what the FastCode Project's CompareMem does
    in assembler code.

    Does the div 4 as apposed to shr 2 use the same number of CPU cycles?

    Anyway, I think this thread should die about now
    ;

    Ross.

    Message
    From: "Malcolm Clark" <malcolm (AT) post3 (DOT) tele.dk>
    To: "Borland's Delphi Discussion List" <delphi (AT) elists (DOT) org>
    Sent: Sunday, November 06, 2005 1:06 AM
    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 ADataToSearch:Pointer; const
    LengthInBytes:LongWord) : boolean;
    var
    p:pLongWord; //pLongWord defined System unit in D7
    i:LongWord;
    begin
    p:=pLongWord(ADataToSearch);
    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 -Delphi (AT) elists (DOT) org
  • No.6 | | 380 bytes | |

    >Does the div 4 as apposed to shr 2 use the same number of CPU cycles?
    Yes, the compiler has generated a shift for division by powers of two as
    far back as Turbo Pascal days.

    >Anyway, I think this thread should die about now

    I'm terminated:-)
    -Malcolm

    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.7 | | 585 bytes | |

    Malcolm Clark wrote:

    p:=pLongWord(ADataToSearch);
    result:=false;
    for i:=0 to ((LengthInBytes div 4)-1) do
    if p^ <0 then exit
    else inc(p);

    I had to activate this thread again as I'm not convinced that inc(p)
    will increment 4 bytes at a time. I'm thinking that p^ will access 4
    bytes, but inc(p) will only increment 1 byte of the block, and then
    access the same 3 bytes overlapped plus 1 new byte. am I wrong?

    I think it will need to be inc(p,4) ?

    Ross.

    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.8 | | 562 bytes | |

    Ross Levis wrote:
    []
    I had to activate this thread again as I'm not convinced that inc(p)
    will increment 4 bytes at a time. I'm thinking that p^ will access 4
    bytes, but inc(p) will only increment 1 byte of the block, and then
    access the same 3 bytes overlapped plus 1 new byte. am I wrong?

    I think it will need to be inc(p,4) ?

    If p is typed as ^Longword it will be incremented 4 bytes at a time. If
    as ^byte, then one byte at a time.

    David

    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.9 | | 913 bytes | |

    I see. So effectively, inc(p) gets translated to inc(p,4) by the
    compiler.

    Ross.

    Message
    From: "David J Taylor" <david-taylor (AT) blueyonder (DOT) co.uk>
    To: "Borland's Delphi Discussion List" <delphi (AT) elists (DOT) org>
    Sent: Monday, November 07, 2005 11:27 PM
    Subject: Re: Test memory block

    Ross Levis wrote:
    []
    I had to activate this thread again as I'm not convinced that inc(p)
    will increment 4 bytes at a time. I'm thinking that p^ will access 4
    bytes, but inc(p) will only increment 1 byte of the block, and then
    access the same 3 bytes overlapped plus 1 new byte. am I wrong?

    I think it will need to be inc(p,4) ?

    If p is typed as ^Longword it will be incremented 4 bytes at a time. If
    as ^byte, then one byte at a time.

    David

    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.10 | | 297 bytes | |

    Ross Levis wrote:
    I see. So effectively, inc(p) gets translated to inc(p,4) by the
    compiler.
    Ross.
    If P is a pointer to a four byte quantity, the operation is
    Pointer (Integer (P) + 4)
    David
    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.11 | | 335 bytes | |

    Ross Levis wrote:
    I see. So effectively, inc(p) gets translated to inc(p,4) by the
    compiler.

    No, Inc(P) gets translated to P := Ptr(Cardinal(P) + S(P^)). Inc(P,
    4) gets translated to P := Ptr(Cardinal(P) + 4 * S(P^)).

    You can see an example of pointers, including the Inc procedure, at the
    Delphi Basics site:
  • No.12 | | 205 bytes | |

    John Dammeyer wrote:
    The only way you are going to know which algorithm is the fastest is to
    study the machine code.
    The way to know which is fastest is to run them and see which runs fastest.

Re: Test memory block


max 4000 letters.
Your nickname that display:
In order to stop the spam: 7 + 6 =
QUESTION ON "Delphi"

EMSDN.COM