[fpc-pascal] Effective memory allocation

Xiangrong Fang xrfang at gmail.com
Sun Nov 2 14:54:40 CET 2014


Hi All,

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:

program getmem;
{$mode objfpc}{$H+}
uses epiktimer;
const
  SIZE = 1024 * 1024 * 1024;
  CNT = 10;
var
  a: array of Byte;
  p: Pointer;
  et: TEpikTimer;
  i: Integer;
  t1, t2: TimerData;
begin
  et := TEpikTimer.Create(nil);
  et.Clear(t1); et.Clear(t2);
  for i := 1 to CNT do begin
    et.Start(t1);
    p := GetMemory(SIZE);
//    SetLength(a, SIZE);
    et.Stop(t1);
    et.Start(t2);
    FillQWord(p^, SIZE div 8, 0);
//    FillQWord(a[0], SIZE div 8, 0);
    et.Stop(t2);
    FreeMem(p);
//    a := nil;
  end;
  WriteLn('Alloc: ', et.Elapsed(t1) / CNT);
  WriteLn('Clear: ', et.Elapsed(t2) / CNT);
end.

The result is:

Using GetMemory:

Alloc:  9.4078169999999997E-0001
Clear:  2.1342020000000002E-0001

Using SetLength:

Alloc:  2.8100000000000000E-0005
Clear:  7.7497550000000004E-0001

It is understandable that GetMemory() is faster than SetLength(), but why
the difference in FillQWord()?

Also, I usually use pointer to pass block of memory to functions.  How do I
implement a function with the following signature:

procedure MyProc(var Buf; Len: Integer):

I mean, how to handle "var Buf" inside the procedure body?

Thanks!

Xiangrong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20141102/0e19bc06/attachment.html>


More information about the fpc-pascal mailing list