Elementary cellular automaton/Random number generator: Difference between revisions
Content added Content deleted
m (→{{header|Pascal}}: Freepascal knows ROR and ROL using 64 Bit on 64-Bit Oprating system is fast) |
m (→{{header|Pascal}}: changed, so that the output ist comparable to C and GO -> 44 at 2000000) |
||
Line 308: | Line 308: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{Works with|Free Pascal}} |
{{Works with|Free Pascal}} |
||
Using ROR and ROL is nearly as fast as assembler and more portable.<BR>[https://tio.run/##7VVtc5pAEP7Or9gPmUFTIpCkaQq1M8aXxBkDVMn05UuGwKk34sFwR0zM5K/X7oEYNfYXtH7Au9vdZ5/dvd1LAx4G8ck4DVcrL0smWTCHYR6TM8NWdH0qRGrpOmGNBZ3RlEQ0aCTZRJc73SFP4n4kAkHupcX9jsk8ENNFksVRY5HEY0RthMlcL5EbUzGPlZejfq/T7UHPa78qAC9Ht0lEoEPidErLA9fz@7f9Xy2/7zrgOlprMHhFD1LUdjvd1qB/7UCaJWHzUhu4rtc0XxG1Oxh1S/uW5/k/vS60XWfkDrqF0On0e69KzglHldEzvxM05rYSJowLPJkHT8MkZxGHJpwem4ZhFB8bRVl5jr8mmMVJO817uDlrfCKfbUBiw@clYfARzAvDAD/PHhIphOvpUlEegwz9l5zPTiXBMhllBsGCO8rExbmtKDIiEuUZgT6jYlvLpiymjNjKA5lQtgdhIS1bISzagThQpApEMsKwcqZhEsmjxlD1jUblAuUAiL3DA889NCnO3eE3LHRUk0BmXYokUCka7It2YpY6tULZHUo/dfiBC8mlimOcs1DQhBVhlLZXzxiqEwj6SPpM2MpVxZPwPBYFZlmuci9dlMsP5V9dOqvt8Gg5HUB6h@7zf5x/E2e/jUYpIZEgXFRt45uab2DX@h208@m86AlqwYGbucioIDGrqRsQGCcZcOmR0yWBZAyqdnGuqfBABVeLRtH170E2hzyVeplYTxPUbHt3KO4hAJVBmsbblDoxIUoWTCRg4EKRo@pQbCh4N1hkRIbEE@tQpIPSw2Yk/gX@rS0LFHMbpQp9V6@@LdrAY/j4EsUcRDAjmBBkgI9Ild6aenNjOY41GjWWy6Wq@eaJb9R3kNTwOYwJh5RkIN1gdVStVkNNYdSPLy/OMU1yZNf1jU/LsE63Md4X3g/4rKr5zAIcjmRCsgPF9acEmfMZlPU7lOEip7OiaoBZXD8o61QWQHuJss4PkatcF9fJL@4krHlWMCp86Tp@d/hVsslIEK2tG6vV73AcBxO@OnHP/gA Try it online!] I don't know the CPU-frequency.Runtime about 57 ms for 2 million |
|||
Fast only in 64 Bit. |
|||
<lang pascal>Program Rule30; |
<lang pascal>Program Rule30; |
||
//http://en.wikipedia.org/wiki/Next_State_Rule_30; |
//http://en.wikipedia.org/wiki/Next_State_Rule_30; |
||
Line 316: | Line 315: | ||
{$Mode Delphi} |
{$Mode Delphi} |
||
{$OPTIMIZATION ON,ALL} |
{$OPTIMIZATION ON,ALL} |
||
{$CODEALIGN proc=8} |
{$CODEALIGN proc=8,LOOP=1} |
||
{$ELSE} |
{$ELSE} |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 334: | Line 333: | ||
begin |
begin |
||
Rule30_State:= 1; |
Rule30_State:= 1; |
||
⚫ | |||
function BinStr(Zahl: Uint64): String; |
|||
var |
|||
i : integer; |
|||
begin |
|||
setlength(result,9); |
|||
⚫ | |||
For i := 0 to 7 do |
|||
begin |
|||
result[7-i+2] := chr(Zahl AND 1+Ord('0')); |
|||
Zahl := Zahl shr 1; |
|||
end; |
|||
end; |
|||
procedure Out_Rule30_State; |
|||
var |
|||
tmpState : Uint64; |
|||
i : integer; |
|||
tmp : array[0..7] of byte; |
|||
begin |
|||
tmpState := Rule30_State; |
|||
//SwapEndian(tmpState); //does not work |
|||
For i := 0 to 7 do |
|||
⚫ | |||
tmp[i] := Byte(tmpState); |
|||
tmpState := tmpState SHR 8; |
|||
end; |
|||
For i := 7 downto 0 do |
|||
write(tmp[i]:4,BinStr(tmp[i])); |
|||
writeln; |
|||
end; |
end; |
||
Line 375: | Line 343: | ||
next := ROLQword(run,1); |
next := ROLQword(run,1); |
||
Rule30_State := (next OR run) XOR prev; |
Rule30_State := (next OR run) XOR prev; |
||
⚫ | |||
function NextRule30Byte:NativeInt; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
result := (result+result) OR (Rule30_State AND 1);Next_State_Rule_30; |
|||
result := (result+result) OR (Rule30_State AND 1);Next_State_Rule_30; |
|||
result := (result+result) OR (Rule30_State AND 1);Next_State_Rule_30; |
|||
result := (result+result) OR (Rule30_State AND 1);Next_State_Rule_30; |
|||
result := (result+result) OR (Rule30_State AND 1);Next_State_Rule_30; |
|||
result := (result+result) OR (Rule30_State AND 1);Next_State_Rule_30; |
|||
end; |
end; |
||
Line 380: | Line 361: | ||
var |
var |
||
T1,T0 : TDateTime; |
T1,T0 : TDateTime; |
||
i |
i: NativeInt; |
||
Begin |
Begin |
||
writeln('Speedtest for statesize of ',64,' bits'); |
writeln('Speedtest for statesize of ',64,' bits'); |
||
//Warm up start Turbo of CPU |
//Warm up start Turbo of CPU |
||
For |
For i := 10*1000*1000-1 downto 0 do |
||
Next_State_Rule_30; |
Next_State_Rule_30; |
||
Line 390: | Line 371: | ||
T0 := time; |
T0 := time; |
||
For i := maxRounds-1 downto 0 do |
For i := maxRounds-1 downto 0 do |
||
NextRule30Byte; |
|||
Begin |
|||
b := 0; |
|||
For j := 7 downto 0 do |
|||
Begin |
|||
⚫ | |||
Next_State_Rule_30; |
|||
end; |
|||
end; |
|||
T1 := time; |
T1 := time; |
||
writeln(NextRule30Byte); |
|||
Out_Rule30_State; |
|||
writeln(maxRounds,' calls take ',FormatDateTime('HH:NN:SS.zzz',T1-T0)); |
writeln(maxRounds,' calls take ',FormatDateTime('HH:NN:SS.zzz',T1-T0)); |
||
writeln('cycles per Byte : ',((T1-t0)*86400*CpuF)/maxRounds:0:2); |
writeln('cycles per Byte : ',((T1-t0)*86400*CpuF)/maxRounds:0:2); |
||
Line 407: | Line 381: | ||
procedure Task; |
procedure Task; |
||
var |
var |
||
k |
k: integer; |
||
Begin |
Begin |
||
writeln('The task '); |
writeln('The task '); |
||
InitRule30_State; |
InitRule30_State; |
||
For k := 1 to rounds |
For k := 1 to rounds |
||
⚫ | |||
Begin |
|||
b := 0; |
|||
For j := 7 downto 0 do |
|||
Begin |
|||
⚫ | |||
Next_State_Rule_30; |
|||
end; |
|||
⚫ | |||
end; |
|||
writeln; |
writeln; |
||
end; |
end; |
||
Line 433: | Line 399: | ||
//running compiled for 64-BIT |
//running compiled for 64-BIT |
||
Speedtest for statesize of 64 bits |
Speedtest for statesize of 64 bits |
||
44 |
|||
231_11100111 204_11001100 6_00000110 122_01111010 253_11111101 204_11001100 220_11011100 230_11100110 |
|||
2000000 calls take 00:00:00.049 |
2000000 calls take 00:00:00.049 |
||
cycles per Byte : 90.65 |
cycles per Byte : 90.65 |
||
The task |
The task |
||
220 197 147 174 117 97 149 171 100 151 |
220 197 147 174 117 97 149 171 100 151 |
||
<ENTER> |
<ENTER> |
||
//running compiled for 32-BIT |
//running compiled for 32-BIT |
||
Speedtest for statesize of 64 bits |
Speedtest for statesize of 64 bits |
||
44 |
|||
231_11100111 204_11001100 6_00000110 122_01111010 253_11111101 204_11001100 220_11011100 230_11100110 |
|||
2000000 calls take 00:00:00. |
2000000 calls take 00:00:00.108 |
||
cycles per Byte : |
cycles per Byte : 199.80 |
||
The task |
The task |