i have a struture 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;
and a procedure to generate random sample data
Procedure FillRandomListSignals(var ListSignals:TListSignals);
var
i :Integer;
begin
for i := 0 to MaxSignalRecords - 1 do
with ListSignals[i] do
begin
signal1 :=Random(MaxInt);
signal2 :=Random(MaxInt);
signal3 :=Random(MaxInt);
signal4 :=Random(MaxInt);
signal5 :=Random(MaxInt);
signal6 :=Random(MaxInt);
bsignal1 :=Boolean(Random(2));
bsignal2 :=Boolean(Random(2));
bsignal3 :=Boolean(Random(2));
bsignal4 :=Boolean(Random(2));
bsignal5 :=Boolean(Random(2));
bsignal6 :=Boolean(Random(2));
end;
end;
How i can improve the performance of the FillRandomListSignals procedure ?
Edit : this structure is used to make thousands(can be millions) of calculations
for i:=1 to 1000000 do
begin
CleartheList(MyList);
FillRandomListSignals(MyList);
DotheMath(MyList);
DotheChart(MyList);
end;
Speed is not your only concern when you generate random data, you actually want that data to be random, you don’t want your experiment to be plagued by repetitive data or other pseudo-random generator problems. If you care more about speed then randomness, you can always use a function like this one, that’ll be ultra-fast!
</joke>.Here’s a post by Barry Kelly on Stack Overflow describing possible problems with the built-in random number generator. Not going to quote it here, go read it for yourself, it’s good stuff.
To get to the conclusion, when I needed a PRNG good enough to generate huge amounts of random data I used a Mersenne Twister (wikipedia link), seeded by Delphi’s PRNG.
Quote from Wikipedia on Mersene Twister:
And to break all my records on the number of links per post, I used this Delphi implementation.
And my final thought: Unless you’re very good with math, stay away from home-made PRNG implementations. Just like hash functions, it’s easy to make a mistake and they’re very difficult to analyze.
Edit
Did some timing, using the following code. The 10,000,000 records took 1480 ms to generate using the Mersenne Twister. The same code, using Delphi’s built in random number generator only took 250 ms, for the same 10M records. Something tells me it’s not the random generator that needs optimizing, but something else in the code.