The kernels, with their Pascal twins test/bench holds three: a heap sort, a sum over a slice and a search for a word in a text, each with check off; and its twin, built by FPC -O2. make kernels times the two of each, the fastest of five runs: today 1.34, 1.75 and 1.2 times FPC's.
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | make raz # the compiler, ./raz; its library is lib/ |
| 12 | 12 | make check # every example and test against its expected output |
| 13 | 13 | make bench # must report at least 400,000 lines a second |
| 14 | + make kernels # test/bench's kernels against FPC -O2's twins | |
| 14 | 15 | make loc # lines of the compiler, by rloc; the last, code, is the budget |
| 15 | 16 | make tests # the small Pascal tests of scan, x64, link and elf |
| 16 | 17 | make gdb # gdb stops at a line of a test and names it |
| @@ -52,6 +52,11 @@ | ||
| 52 | 52 | echo "$$lines lines in $$((best / 1000000)) ms: $$rate lines a second"; \ |
| 53 | 53 | test $$rate -ge 400000 |
| 54 | 54 | |
| 55 | +# the code's speed: the kernels of test/bench, which say check off, | |
| 56 | +# against their twins in Pascal built by FPC -O2 | |
| 57 | +kernels: raz | |
| 58 | + test/bench/run.sh | |
| 59 | + | |
| 55 | 60 | # native threads under load: 40,000 spawned and joined, 200 alive at a |
| 56 | 61 | # time, all waiting on one Mutex; then, with little memory, spawn must |
| 57 | 62 | # fail with Thread.Failed. make check runs a small version. |
| @@ -114,4 +119,4 @@ | ||
| 114 | 119 | rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(MAN)/man1/raz.1 $(MAN)/man3/raz-lib.3 \ |
| 115 | 120 | $(MAN)/man7/raz.7 $(MAN)/man7/raz-tutorial.7 |
| 116 | 121 | |
| 117 | -.PHONY: units tests check gdb bench stress fmt loc man pdf clean install uninstall | |
| 122 | +.PHONY: units tests check gdb bench kernels stress fmt loc man pdf clean install uninstall |
| @@ -11,10 +11,10 @@ | ||
| 11 | 11 | ## Now: better code again (v0.8.0) |
| 12 | 12 | |
| 13 | 13 | For: on the same program, raz within 1.3 times the time of FPC `-O2` |
| 14 | -with `check off;` (today 1.3 on a heap sort, 1.7 on a sum over a slice, | |
| 15 | -1.8 on a search: these kernels go in | |
| 16 | -`test/bench` first, with their Pascal twins, so anyone can measure | |
| 17 | -them), and every default program smaller; the compiler as clear as | |
| 14 | +with `check off;` (on `test/bench`'s kernels, which `make kernels` | |
| 15 | +measures against their Pascal twins: today 1.34 on a heap sort, 1.75 | |
| 16 | +on a sum over a slice, 1.2 on a search for a word in a text), and | |
| 17 | +every default program smaller; the compiler as clear as | |
| 18 | 18 | v0.7.0 left it, and under 10,000 lines. No change to the language. |
| 19 | 19 | Each step is measured as v0.7.0's were, on rat and on its kernel, and |
| 20 | 20 | one that buys less than 2% on both is reverted and noted here. |
| @@ -0,0 +1,61 @@ | ||
| 1 | +(* A heap sort of pseudo-random numbers. *) | |
| 2 | +module Heapsort; | |
| 3 | +import Out; | |
| 4 | +check off; | |
| 5 | + | |
| 6 | +const | |
| 7 | + N = 200000; | |
| 8 | + Rounds = 8; | |
| 9 | + | |
| 10 | +var a: [N]INT; | |
| 11 | + | |
| 12 | +(* s[lo] sifted down the heap of s[0..hi] *) | |
| 13 | +proc sift(s: []INT; lo, hi: INT); | |
| 14 | +var root, child, t: INT; | |
| 15 | +begin | |
| 16 | + root := lo; | |
| 17 | + child := 2 * root + 1; | |
| 18 | + while child < hi do | |
| 19 | + if (child + 1 < hi) and (s[child] < s[child + 1]) then INC(child); end; | |
| 20 | + if s[root] >= s[child] then return; end; | |
| 21 | + t := s[root]; | |
| 22 | + s[root] := s[child]; | |
| 23 | + s[child] := t; | |
| 24 | + root := child; | |
| 25 | + child := 2 * root + 1; | |
| 26 | + end; | |
| 27 | +end; | |
| 28 | + | |
| 29 | +proc sort(s: []INT); | |
| 30 | +var i, t: INT; | |
| 31 | +begin | |
| 32 | + for i := LEN(s) div 2 - 1 to 0 by -1 do sift(s, i, LEN(s)); end; | |
| 33 | + for i := LEN(s) - 1 to 1 by -1 do | |
| 34 | + t := s[0]; | |
| 35 | + s[0] := s[i]; | |
| 36 | + s[i] := t; | |
| 37 | + sift(s, 0, i); | |
| 38 | + end; | |
| 39 | +end; | |
| 40 | + | |
| 41 | +proc main*; | |
| 42 | +var i, r, x, total: INT; | |
| 43 | +begin | |
| 44 | + x := 1; | |
| 45 | + total := 0; | |
| 46 | + for r := 1 to Rounds do | |
| 47 | + for i := 0 to N - 1 do | |
| 48 | + x := (x * 1103515245 + 12345) & 7FFFFFFFH; | |
| 49 | + a[i] := x; | |
| 50 | + end; | |
| 51 | + sort(a); | |
| 52 | + for i := 1 to N - 1 do | |
| 53 | + if a[i - 1] > a[i] then Out.str("not sorted\n"); end; | |
| 54 | + end; | |
| 55 | + total := total + a[N div 2]; | |
| 56 | + end; | |
| 57 | + Out.int(total); | |
| 58 | + Out.ln; | |
| 59 | +end; | |
| 60 | + | |
| 61 | +end Heapsort. |
| @@ -0,0 +1,42 @@ | ||
| 1 | +(* A search: where a word first is in a text, by trying each place in | |
| 2 | + turn, over a text of four letters. *) | |
| 3 | +module Search; | |
| 4 | +import Out; | |
| 5 | +check off; | |
| 6 | + | |
| 7 | +const | |
| 8 | + N = 100000; | |
| 9 | + Rounds = 400; | |
| 10 | + | |
| 11 | +var text: [N]U8; | |
| 12 | + | |
| 13 | +(* where sub first starts in s, or -1 *) | |
| 14 | +proc find(s, sub: []U8): INT; | |
| 15 | +var i, j: INT; | |
| 16 | +begin | |
| 17 | + for i := 0 to LEN(s) - LEN(sub) do | |
| 18 | + j := 0; | |
| 19 | + while (j < LEN(sub)) and (s[i + j] = sub[j]) do INC(j); end; | |
| 20 | + if j = LEN(sub) then return i; end; | |
| 21 | + end; | |
| 22 | + return -1; | |
| 23 | +end; | |
| 24 | + | |
| 25 | +proc main*; | |
| 26 | +var i, r, x, total: INT; word: [16]U8; | |
| 27 | +begin | |
| 28 | + x := 1; | |
| 29 | + for i := 0 to N - 1 do | |
| 30 | + x := (x * 1103515245 + 12345) & 7FFFFFFFH; | |
| 31 | + text[i] := U8('a' + (x shr 16) & 3); | |
| 32 | + end; | |
| 33 | + total := 0; | |
| 34 | + for r := 1 to Rounds do | |
| 35 | + COPY(word, text[N - 16 - r..N - r]); | |
| 36 | + total := total + find(text, word); | |
| 37 | + end; | |
| 38 | + Out.int(total); | |
| 39 | + Out.ln; | |
| 40 | +end; | |
| 41 | + | |
| 42 | +end Search. |
| @@ -0,0 +1,30 @@ | ||
| 1 | +(* A sum over a slice: the loop of a procedure that takes one. *) | |
| 2 | +module Sum; | |
| 3 | +import Out; | |
| 4 | +check off; | |
| 5 | + | |
| 6 | +const | |
| 7 | + N = 1000000; | |
| 8 | + Rounds = 300; | |
| 9 | + | |
| 10 | +var a: [N]INT; | |
| 11 | + | |
| 12 | +proc sum(s: []INT): INT; | |
| 13 | +var i, t: INT; | |
| 14 | +begin | |
| 15 | + t := 0; | |
| 16 | + for i := 0 to LEN(s) - 1 do t := t + s[i]; end; | |
| 17 | + return t; | |
| 18 | +end; | |
| 19 | + | |
| 20 | +proc main*; | |
| 21 | +var i, r, total: INT; | |
| 22 | +begin | |
| 23 | + for i := 0 to N - 1 do a[i] := i; end; | |
| 24 | + total := 0; | |
| 25 | + for r := 1 to Rounds do total := total + sum(a); end; | |
| 26 | + Out.int(total); | |
| 27 | + Out.ln; | |
| 28 | +end; | |
| 29 | + | |
| 30 | +end Sum. |
| @@ -0,0 +1,53 @@ | ||
| 1 | +{ Heapsort.raz's twin. } | |
| 2 | +program heapsort; | |
| 3 | +{$mode objfpc} | |
| 4 | +const | |
| 5 | + N = 200000; | |
| 6 | + Rounds = 8; | |
| 7 | +var | |
| 8 | + a: array[0..N - 1] of int64; | |
| 9 | + i, r, x, total: int64; | |
| 10 | + | |
| 11 | +procedure Sift(var s: array of int64; lo, hi: int64); | |
| 12 | +var root, child, t: int64; | |
| 13 | +begin | |
| 14 | + root := lo; | |
| 15 | + child := 2 * root + 1; | |
| 16 | + while child < hi do begin | |
| 17 | + if (child + 1 < hi) and (s[child] < s[child + 1]) then inc(child); | |
| 18 | + if s[root] >= s[child] then exit; | |
| 19 | + t := s[root]; | |
| 20 | + s[root] := s[child]; | |
| 21 | + s[child] := t; | |
| 22 | + root := child; | |
| 23 | + child := 2 * root + 1; | |
| 24 | + end; | |
| 25 | +end; | |
| 26 | + | |
| 27 | +procedure Sort(var s: array of int64); | |
| 28 | +var i, t: int64; | |
| 29 | +begin | |
| 30 | + for i := Length(s) div 2 - 1 downto 0 do Sift(s, i, Length(s)); | |
| 31 | + for i := High(s) downto 1 do begin | |
| 32 | + t := s[0]; | |
| 33 | + s[0] := s[i]; | |
| 34 | + s[i] := t; | |
| 35 | + Sift(s, 0, i); | |
| 36 | + end; | |
| 37 | +end; | |
| 38 | + | |
| 39 | +begin | |
| 40 | + x := 1; | |
| 41 | + total := 0; | |
| 42 | + for r := 1 to Rounds do begin | |
| 43 | + for i := 0 to N - 1 do begin | |
| 44 | + x := (x * 1103515245 + 12345) and $7FFFFFFF; | |
| 45 | + a[i] := x; | |
| 46 | + end; | |
| 47 | + Sort(a); | |
| 48 | + for i := 1 to N - 1 do | |
| 49 | + if a[i - 1] > a[i] then writeln('not sorted'); | |
| 50 | + total := total + a[N div 2]; | |
| 51 | + end; | |
| 52 | + writeln(total); | |
| 53 | +end. |
| @@ -0,0 +1,27 @@ | ||
| 1 | +#!/bin/sh | |
| 2 | +# The kernels: each raz program here, which says check off, against its | |
| 3 | +# twin in Pascal, built by FPC -O2; each run five times, the fastest | |
| 4 | +# kept. Run from the directory above test/: | |
| 5 | +# test/bench/run.sh [compiler] | |
| 6 | +raz=${1:-./raz} | |
| 7 | +out=build/bench | |
| 8 | +mkdir -p $out | |
| 9 | + | |
| 10 | +best() { # best program: the fewest milliseconds of five runs | |
| 11 | + b=0 | |
| 12 | + for i in 1 2 3 4 5; do | |
| 13 | + s=$(date +%s%N); $1 > /dev/null; e=$(date +%s%N) | |
| 14 | + t=$(( (e - s) / 1000000 )) | |
| 15 | + if [ $b = 0 ] || [ $t -lt $b ]; then b=$t; fi | |
| 16 | + done | |
| 17 | + echo $b | |
| 18 | +} | |
| 19 | + | |
| 20 | +for f in test/bench/*.raz; do | |
| 21 | + n=$(basename $f .raz) | |
| 22 | + $raz -o $out/$n $f || exit 1 | |
| 23 | + fpc -O2 -v0 -FE$out -o$out/$n.fpc test/bench/$(echo $n | tr A-Z a-z).pas > /dev/null || exit 1 | |
| 24 | + if [ "$($out/$n)" != "$($out/$n.fpc)" ]; then echo "$n: the two print differently"; exit 1; fi | |
| 25 | + r=$(best $out/$n); p=$(best $out/$n.fpc) | |
| 26 | + echo "$n $r $p" | awk '{ printf "%-10s raz %5d ms fpc -O2 %5d ms %.2f times\n", $1, $2, $3, $2 / $3 }' | |
| 27 | +done |
| @@ -0,0 +1,35 @@ | ||
| 1 | +{ Search.raz's twin. } | |
| 2 | +program search; | |
| 3 | +{$mode objfpc} | |
| 4 | +const | |
| 5 | + N = 100000; | |
| 6 | + Rounds = 400; | |
| 7 | +var | |
| 8 | + text: array[0..N - 1] of byte; | |
| 9 | + word: array[0..15] of byte; | |
| 10 | + i, r, x, total: int64; | |
| 11 | + | |
| 12 | +function Find(const s, sub: array of byte): int64; | |
| 13 | +var i, j: int64; | |
| 14 | +begin | |
| 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; | |
| 21 | +end; | |
| 22 | + | |
| 23 | +begin | |
| 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); | |
| 35 | +end. |
| @@ -0,0 +1,24 @@ | ||
| 1 | +{ Sum.raz's twin: a sum over an open array, its slice. } | |
| 2 | +program sum; | |
| 3 | +{$mode objfpc} | |
| 4 | +const | |
| 5 | + N = 1000000; | |
| 6 | + Rounds = 300; | |
| 7 | +var | |
| 8 | + a: array[0..N - 1] of int64; | |
| 9 | + i, r, total: int64; | |
| 10 | + | |
| 11 | +function Sum(const s: array of int64): int64; | |
| 12 | +var i, t: int64; | |
| 13 | +begin | |
| 14 | + t := 0; | |
| 15 | + for i := 0 to High(s) do t := t + s[i]; | |
| 16 | + Sum := t; | |
| 17 | +end; | |
| 18 | + | |
| 19 | +begin | |
| 20 | + for i := 0 to N - 1 do a[i] := i; | |
| 21 | + total := 0; | |
| 22 | + for r := 1 to Rounds do total := total + Sum(a); | |
| 23 | + writeln(total); | |
| 24 | +end. |