raz / test / bench / Lines.raz

46 lines · 1.0 KiB · raw · history · last change 264716cb

1(* The lines of a text, counted all at once with Bytes.count, and walked
2 one by one with Bytes.index: lines.pas does the same with FPC's
3 IndexByte, which is assembly, and a counting loop. *)
4module Lines;
5import Out, Bytes;
6import SYSTEM;
7check off;
8
9const
10 N = 16777216;
11 Rounds = 32;
12 Letters = "etaoinshrdlucmfw";
13
14var text: [N]U8;
15
16proc main*;
17var i, r, x, v, k, total, walked: INT;
18begin
19 x := 1;
20 for i := 0 to N - 1 do
21 x := (x * 1103515245 + 12345) & 7FFFFFFFH;
22 v := (x shr 16) & 63;
23 if v = 0 then text[i] := 10;
24 elsif v < 10 then text[i] := U8(' ');
25 else text[i] := Letters[v & 15];
26 end;
27 end;
28 total := 0;
29 walked := 0;
30 for r := 1 to Rounds do
31 total := total + Bytes.count(text, 10);
32 i := 0;
33 k := Bytes.index(text, 10);
34 while k >= 0 do
35 INC(walked);
36 i := i + k + 1;
37 k := Bytes.index(text[i..N], 10);
38 end;
39 end;
40 Out.int(total);
41 Out.str(" ");
42 Out.int(walked);
43 Out.ln;
44end;
45
46end Lines.