kemi (statz) | #2, Főfasz (2970) |
3677 | #246b | ^ | Idézet | Tue, 19 Mar 2013 17:55:10 +01 |
188.143.*.* | *.pool.digikabel.hu |
Kíváncsi voltam melyik fordít hatékonyabb kódot, az fpc, vagy a gcc. rnd2.pas unit Rnd2; interface procedure srnd2(seed: Longint); function rand2: Longint; implementation var __seed0, __seed1, __seed2, __seed3: Longint; function __rol(value: Longint; offset: Byte): Longint; begin offset := offset and $1F; __rol := (value shl offset) or (value shr ($20 - offset)); end; function __ror(value: Longint; offset: Byte): Longint; begin offset := offset and $1F; __ror := (value shr offset) or (value shl ($20 - offset)); end; procedure srnd2(seed: Longint); begin __seed0 := seed; __seed1 := __seed0 xor $AAAAAAAA; __seed1 := __rol(__seed1 shr 11, __seed1); __seed1 := __seed1 xor (__seed1 shr 21); __seed2 := __seed0 xor $55555555; __seed2 := __ror(__seed2 shl 6, __seed2); __seed2 := __seed2 xor (__seed2 shr 22); __seed3 := (__seed2 shl 16) or (__seed1 shr 16); end; function rand2: Longint; begin __seed1 := __rol(__seed1, 5); __seed2 := __ror(__seed2, 9); __seed0 := __rol(__seed0, 11); __seed0 := __seed0 xor __seed1; __seed0 := __seed0 xor __rol(__seed1, 8) shl 11; __seed0 := __seed0 xor __ror(__seed2, 8) and 63; __seed0 := __seed0 xor __rol(__seed3, 8) shr 5; __seed0 := __ror(__seed0, 14); __seed0 := __seed0 xor __seed3; __seed1 := __seed0 xor __seed3; __seed2 := __seed0 xor __seed3; __seed3 := __ror(__seed3, __seed0); rand2 := __seed0; end; end. pas.pas program pas; uses Crt, Rnd2, SysUtils, DateUtils; var st: TSystemTime; table: Array[0..255] of Integer; i, j, k, r, temp: Integer; begin DateTimeToSystemTime(Now, st); srnd2(st.MilliSecond); for i := 0 to 999 do begin for j := 0 to 999 do begin for k := 0 to 255 do table[k] := k; for k := 0 to 255 do begin r := rand2 and 255; temp := table[r]; table[k] := temp; end; end; end; end. PS C:\Users\kemi242\Desktop\csc> measure-command {.\c.exe} Days : 0 Hours : 0 Minutes : 0 Seconds : 20 Milliseconds : 646 Ticks : 206461548 TotalDays : 0,000238960125 TotalHours : 0,005735043 TotalMinutes : 0,34410258 TotalSeconds : 20,6461548 TotalMilliseconds : 20646,1548 PS C:\Users\kemi242\Desktop\csc> measure-command {.\pas.exe} Days : 0 Hours : 0 Minutes : 0 Seconds : 23 Milliseconds : 625 Ticks : 236252006 TotalDays : 0,000273439821759259 TotalHours : 0,00656255572222222 TotalMinutes : 0,393753343333333 TotalSeconds : 23,6252006 TotalMilliseconds : 23625,2006 PS C:\Users\kemi242\Desktop\csc> measure-command {.\c_O3.exe} Days : 0 Hours : 0 Minutes : 0 Seconds : 0 Milliseconds : 938 Ticks : 9389131 TotalDays : 1,08670497685185E-05 TotalHours : 0,000260809194444444 TotalMinutes : 0,0156485516666667 TotalSeconds : 0,9389131 TotalMilliseconds : 938,9131 PS C:\Users\kemi242\Desktop\csc> measure-command {.\pas_O3.exe} Days : 0 Hours : 0 Minutes : 0 Seconds : 21 Milliseconds : 641 Ticks : 216414951 TotalDays : 0,000250480267361111 TotalHours : 0,00601152641666667 TotalMinutes : 0,360691585 TotalSeconds : 21,6414951 TotalMilliseconds : 21641,4951 Konklúzió: Hát bizony a Pascalos verzió lassabb lett, és az optimalizáció is csak néhány másodpercet dobott rajta. De nembaj, itt az rnd2 Pascal implementációja, használjátok egész seggel. |