raz / test / bench / search.pas

35 lines · 745 B · raw · history · last change 62a9b275

1{ Search.raz's twin. }
2program search;
3{$mode objfpc}
4const
5 N = 100000;
6 Rounds = 400;
7var
8 text: array[0..N - 1] of byte;
9 word: array[0..15] of byte;
10 i, r, x, total: int64;
11
12function Find(const s, sub: array of byte): int64;
13var i, j: int64;
14begin
15 for i := 0 to Length(s) - Length(sub) do begin
16 j := 0;
17 while (j < Length(sub)) and (s[i + j] = sub[j]) do inc(j);
18 if j = Length(sub) then exit(i);
19 end;
20 Find := -1;
21end;
22
23begin
24 x := 1;
25 for i := 0 to N - 1 do begin
26 x := (x * 1103515245 + 12345) and $7FFFFFFF;
27 text[i] := ord('a') + (x shr 16) and 3;
28 end;
29 total := 0;
30 for r := 1 to Rounds do begin
31 Move(text[N - 16 - r], word, 16);
32 total := total + Find(text, word);
33 end;
34 writeln(total);
35end.