Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I quickly clear a record of simple types?

Tags:

delphi

I have a structure defined like this:

const
  MaxSignalRecords=255;
type
  TSignalRecord=record
   signal1  : integer;
   signal2  : integer;
   signal3  : integer;
   signal4  : integer;
   signal5  : integer;
   signal6  : integer;
   bsignal1 : Boolean;
   bsignal2 : Boolean;
   bsignal3 : Boolean;
   bsignal4 : Boolean;
   bsignal5 : Boolean;
   bsignal6 : Boolean;
  end;

TListSignals = Array[0..MaxSignalRecords-1] of TSignalRecord;

This structure is used to make thousands of calculations in an algorithm like this:

for i:=1 to 900000 do
begin
  CleartheList(MyList);
  DotheMath(MyList);
  DotheChart(MyList);
end;

I am looking for a fast way to initializate the values of my TListSignals to 0 and false.

Now I am using this :

procedure ClearListSignals(var ListSignals:TListSignals);
var
  i :Integer;
begin
  for i := 0 to MaxSignalRecords - 1 do
  with ListSignals[i] do
  begin
   signal1   :=0;
   signal2   :=0;
   signal3   :=0;
   signal4   :=0;
   signal5   :=0;
   signal6   :=0;
   bsignal1  :=false;
   bsignal2  :=false;
   bsignal3  :=false;
   bsignal4  :=false;
   bsignal5  :=false;
   bsignal6  :=false;
  end;
end;

How can I improve the performance of the ClearListSignals procedure?

like image 613
Salvador Avatar asked Apr 01 '11 05:04

Salvador


2 Answers

you can use the ZeroMemory procedure located in the Windows unit.

var
  MyList : TListSignals;
begin
     ZeroMemory(@Mylist,SizeOf(MyList));
end;
like image 114
RRUZ Avatar answered Sep 26 '22 08:09

RRUZ


You should use the standard Delphi language function Default()!

const
  MaxSignalRecords = 255;

type
  TSignalRecord = record
    ...
    bsignal6 : Boolean;
  end;

var
  X: TSignalRecord
...
X := Default(TSignalRecord);
...

And no hack.. as suggested before..

like image 39
Z.B. Avatar answered Sep 22 '22 08:09

Z.B.