<p>Am 02.11.2014 14:55 schrieb "Xiangrong Fang" <<a href="mailto:xrfang@gmail.com">xrfang@gmail.com</a>>:<br>
><br>
> Hi All,<br>
><br>
> I am programming a Bloom Filter and need a high-performance way to allocate and wipe large block of memory. I did the following test:<br>
><br>
> program getmem;<br>
> {$mode objfpc}{$H+}<br>
> uses epiktimer;<br>
> const<br>
> SIZE = 1024 * 1024 * 1024;<br>
> CNT = 10;<br>
> var<br>
> a: array of Byte;<br>
> p: Pointer;<br>
> et: TEpikTimer;<br>
> i: Integer;<br>
> t1, t2: TimerData;<br>
> begin<br>
> et := TEpikTimer.Create(nil);<br>
> et.Clear(t1); et.Clear(t2);<br>
> for i := 1 to CNT do begin<br>
> et.Start(t1);<br>
> p := GetMemory(SIZE);<br>
> // SetLength(a, SIZE);<br>
> et.Stop(t1);<br>
> et.Start(t2);<br>
> FillQWord(p^, SIZE div 8, 0);<br>
> // FillQWord(a[0], SIZE div 8, 0);<br>
> et.Stop(t2);<br>
> FreeMem(p);<br>
> // a := nil;<br>
> end;<br>
> WriteLn('Alloc: ', et.Elapsed(t1) / CNT);<br>
> WriteLn('Clear: ', et.Elapsed(t2) / CNT);<br>
> end.<br>
><br>
> The result is:<br>
><br>
> Using GetMemory:<br>
><br>
> Alloc: 9.4078169999999997E-0001<br>
> Clear: 2.1342020000000002E-0001<br>
><br>
> Using SetLength:<br>
><br>
> Alloc: 2.8100000000000000E-0005<br>
> Clear: 7.7497550000000004E-0001<br>
><br>
> It is understandable that GetMemory() is faster than SetLength(), but why the difference in FillQWord()?</p>
<p>If you use SetLength the dynamic array consists not only of the array data, but also of an information record in front of it. This will likely lead to the data not being aligned correctly (FillQWord works best with 8-Byte alignment). So what about testing FillDWord or FillChar? Just to see whether they would be faster. </p>
<p>><br>
> Also, I usually use pointer to pass block of memory to functions. How do I implement a function with the following signature:<br>
><br>
> procedure MyProc(var Buf; Len: Integer):<br>
><br>
> I mean, how to handle "var Buf" inside the procedure body?</p>
<p>You need to cast Buf to a pointer if I remember correctly. Take a look at the implementation of a TStream's descendant's Write method (e.g. TStringStream) for an example.</p>
<p>Regards,<br>
Sven</p>