To prove I have a sense of humour, SieveAhl is written in Modula-2. Yes, that *is* pretty funny, isn't it?
VAR ticks: LONGINT; PROCEDURE TickCount(): LONGINT; CODE 0A975H; PROCEDURE Random(): INTEGER; CODE 0A861H; PROCEDURE BoundedTicks(tick1, tick2: LONGINT); (* needed because of some weirdness with tick2 < tick1! *) BEGIN; IF (tick1 < tick2) THEN ticks := tick2 - tick1; ELSE ticks := tick1 - tick2; END; END BoundedTicks;
rnd()
generates
floating point), the variable r
was made integer too, not real.
[Strictly speaking, it could overflow, not being longint, but audaciously I
assumed that the net sum of all numbers returned by the Toolbox random
number generator would be zero and in practise this seems to be true.]
PROCEDURE DoAhlsBenchMark(iterations: LONGINT): INTEGER; (* 10 rem Ahl's simple benchmark 20 for n = 1 to 100: a = n 30 for i = 1 to 10 40 a = sqr(a): r = r + rnd(1) 50 next i 60 for i = 1 to 10 70 a = a^2: r = r + rnd(1) 80 next i 90 s = s + a: next n 100 print "Accuracy ";abs (1010-s/5) 110 print "Random ";abs (1000-r) *) VAR i, r: INTEGER; a: REAL; s: LONGREAL; n, firsttick: LONGINT; lasttick: LONGINT; BEGIN; firsttick := TickCount(); n:=0D; (* FOR ... *) REPEAT; a:= FLOAT(n); FOR i:=1 TO 10 DO a:=Sqrt(a); r:=r+Random(); END; FOR i:=1 TO 10 DO a:=a*a; r:=r+Random(); END; s:=s+LONG(a); INC(n); UNTIL n = iterations; lasttick := TickCount(); BoundedTicks(firsttick, lasttick); RETURN 0; END DoAhlsBenchMark;
The number of primes in this interval is 1,899 and this is returned as a "checksum" to verify operation. The Sieve of Eratosthenes is a pure integer benchmark.
PROCEDURE DoSieveBenchMark(iterations: LONGINT): INTEGER; VAR iter, firsttick, lasttick: LONGINT; count, i, k, prime: INTEGER; flags: ARRAY[0..8190] OF BOOLEAN; BEGIN; firsttick := TickCount(); iter := 0D; (* FOR ... *) REPEAT; count := 0; FOR i:=0 TO 8190 DO; flags[i] := TRUE; END; FOR i:=0 TO 8190 DO; IF (flags[i]) THEN prime := i+i+3; k:=i+prime; WHILE (k <= 8190) DO flags[k] := FALSE; k := k+prime; END; count := count + 1; END; END; INC(iter); UNTIL iter = iterations; lasttick := TickCount(); BoundedTicks(firsttick, lasttick); RETURN count; END DoSieveBenchMark;