[comp.lang.forth] Byte benchmark sieve

wmb@SUN.COM (11/07/89)

Here are both versions of the sieve benchmark:

decimal
8192 constant size
variable flags   size allot

\ This is the "standard" sieve benchmark as published in Byte Magazine.
\ The algorithm is wrong in the sense that it gives an incorrect count
\ of the number of primes.  That doesn't affect it's usefulness as a
\ benchmark.
\ This benchmark tends to be relatively insensitive to the efficiency
\ of "nesting" (calling a colon definition), since it is implemented
\ almost entirely with very low level words, which are code words in
\ most Forth implementations.  This is reasonably fair, however, since
\ studies have shown that in many Forth programs, code words get
\ executed on the order of 8 times more frequently than colon
\ definitions.
\ Times are generally quoted for 10 iterations (use 10-times).
: do-prime  ( 16 seconds for 32-bit dtc forth)
  flags  size 1  fill  ( set array )
  0 ( 0 count ) size  0
  do  flags  i + c@
    if  i  dup  + 3 + dup i +
    begin  dup  size <
       while  0  over  flags  + c!  over  +
    repeat
   drop drop 1+
   then
 loop
 .  ." primes "
;
: 10-times  10 0 do do-prime loop ;

\ This is the "Colburn Sieve" as published in a letter to the editor
\ of Dr. Dobbs' Journal.  It is the same algorithm as the first, but
\ is a better Forth implementation of the algorithm.  It uses a
\ DO .. LOOP in the inner loop instead of  BEGIN .. WHILE .. REPEAT
\ This version is a more fair comparison of Forth in relation to other
\ languages.  For comparisons between different Forth systems, both
\ versions are widely used.  It is necessary to state which version
\ you are using in order for your benchmark to be useful.
\ The Colburn Sieve typically runs in about 60% of the time of the
\ Byte sieve.
: do-prime.hi  ( 11 seconds for 32-bit dtc forth )
  flags  size 1  fill  ( set array )
  0 ( 0 count ) size  0
  do  flags  i + c@
    if  3 i + i + dup i + size <
       if size flags +  over i +   flags +
            do   0 i c!  dup
            +loop
       then drop  1+  ( bump prime counter)
   then
 loop
 .  ." primes "
;
: 10-times.hi  10 0 do do-prime.hi loop ;