[fpc-pascal] Why is Random(255) some 529x slower compared to Delphi 7?

Florian Klaempfl florian at freepascal.org
Fri Dec 9 09:19:53 CET 2011


Am 09.12.2011 09:02, schrieb Vincent Snijders:
> 2011/12/7 Graeme Geldenhuys <graemeg.lists at gmail.com>:
>> Hi,
>>
>> I did a simple GetTickCount() timing around this loop. Delphi executes
>> the loop in 20 ticks. FPC 2.6.0-rc2 takes 10585 ticks!!!! The outer
>> loop runs 200400 iterations. The types for BitValue, ByteValue and
>> RandSeed is of type Byte.
>>
>> 01  for Index := 1 to Length(Source) do
>> 02  begin
>> 03    OrdValue := Ord(Source[Index]);
>> 04    for BitCount := 0 to 7 do
>> 05    begin
>> 06      BitValue := Byte(OrdValue and (1 shl BitCount) = 1 shl BitCount);
>> 07      RandSeed := ByteValue;
>> 08      ByteValue := (((Random(255) + 1) div 2) * 2) + BitValue;
>> 09      Result[(Index - 1) * 8 + BitCount + 1] := AnsiChar(ByteValue);
>> 10    end;
>> 11  end;
>>
> 
> I have one question about this code, why is RandSeed set inside the
> loop and not outside the loop or even at the program start?
> If you change the randseed, then the random number generator has to be
> initialized and that is more costly for a stateful RNG as the mersenne
> twister.

Oops, mails crossed. The assignment to randseed is indeed the problem.
Why is it done? Bad random generator of delphi :)?



More information about the fpc-pascal mailing list