File: random_speed_test.lpr

package info (click to toggle)
castle-game-engine 6.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 194,520 kB
  • sloc: pascal: 364,585; ansic: 8,606; java: 2,851; objc: 2,601; cpp: 1,412; xml: 851; makefile: 725; sh: 563; php: 26
file content (109 lines) | stat: -rw-r--r-- 3,133 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
program random_speed_test;

uses SysUtils, CastleRandom;

var
  Seed: LongInt = 233489679;

function Raw(N: LongInt): LongInt;
begin
  Seed := ((Seed xor (Seed shl 1)) xor ((Seed xor (Seed shl 1)) shr 15)) xor
         (((Seed xor (Seed shl 1)) xor ((Seed xor (Seed shl 1)) shr 15)) shl 4);
  if N>1 then
    Result := LongInt((Int64(LongWord(Seed))*N) shr 32)
  else
    Result := 0
end;

const
  NTests = 300000000;

var
  i: Integer;
  Rnd: TCastleRandom;
  Sum: Double;
  SumInt: Integer;
  BiasInt, BiasFloat: Integer;
  T: TDateTime;

begin
  Rnd := TCastleRandom.Create;
  try
    SumInt := 0;
    for i:= 1 to NTests do SumInt += 1;
    {Warm-up the CPU (if any power-saving mode is active)
     and skip program post-compillation lags, otherwise
     we might get incorrect results at first tests!}

    Sum := 0;
    T := Now;
    for i := 1 to NTests do
      Sum += 0.5;
    BiasFloat := Round((Now-T)*24*60*60*1000);
    SumInt := 0;
    T := Now;
    for i := 1 to NTests do
      SumInt += 1;
    biasInt := Round((Now-T)*24*60*60*1000);
    WriteLn('Bias ineger = ',BiasInt,' float = ',BiasFloat);

    Sum := 0;
    T := Now;
    for i := 1 to NTests do
      Sum += Random;
    WriteLn('SysUtils float random   t = ',Round((Now-T)*24*60*60*1000)-BiasFloat,' ms average = ',Sum/NTests);

    Sum := 0;
    T := Now;
    for i := 1 to NTests do
      Sum += Rnd.Random;
    WriteLn('Castle float random     t = ',Round((Now-T)*24*60*60*1000)-BiasFloat,' ms average = ',Sum/NTests);

    SumInt := 0;
    T := Now;
    for i := 1 to NTests do
      SumInt += Random(2);
    WriteLn('SysUtils integer random t = ',Round((Now-T)*24*60*60*1000)-BiasInt,' ms average = ',SumInt/NTests);

    SumInt := 0;
    T := Now;
    for i := 1 to NTests do
      SumInt += Rnd.Random(2);
    WriteLn('Castle integer random   t = ',Round((Now-T)*24*60*60*1000)-BiasInt,' ms average = ',SumInt/NTests);

    SumInt := 0;
    T := Now;
    for i := 1 to NTests do
      SumInt += Rnd.RandomInt64(2);
    WriteLn('Castle int64   random   t = ',Round((Now-T)*24*60*60*1000)-BiasInt,' ms average = ',SumInt/NTests);

    SumInt := 0;
    T := Now;
    for i := 1 to NTests do
      SumInt += Raw(2);
    WriteLn('Raw integer Xorshift    t = ',Round((Now-T)*24*60*60*1000)-BiasInt,' ms average = ',SumInt/NTests);


    SumInt := 0;
    T := Now;
    for i := 1 to NTests do if Rnd.Random(2)=0 then inc(SumInt);
    WriteLn('Integer boolean t = ',Round((Now-T)*24*60*60*1000)-BiasInt,' ms average = ',SumInt/NTests);
    SumInt := 0;
    T := Now;
    for i := 1 to NTests do if Rnd.RandomBoolean then inc(SumInt);
    WriteLn('Random boolean t = ',Round((Now-T)*24*60*60*1000)-BiasInt,' ms average = ',SumInt/NTests);

    SumInt := 0;
    T := Now;
    for i := 1 to NTests do SumInt += Rnd.Random(3)-1;
    WriteLn('Integer sign t = ',Round((Now-T)*24*60*60*1000)-BiasInt,' ms average = ',SumInt/NTests);
    SumInt := 0;
    T := Now;
    for i := 1 to NTests do SumInt += Rnd.RandomSign;
    WriteLn('Random sign t = ',Round((Now-T)*24*60*60*1000)-BiasInt,' ms average = ',SumInt/NTests);

  finally
    FreeAndNil(Rnd);
  end;
end.