sieve.mlton

Stephen Weeks MLton@sourcelight.com
Fri, 24 Aug 2001 09:51:27 -0700


> Hi Doug.  I see that sieve.mlton was recently changed (back) to use Word8Array
> instead of bool array.  I find that version to be less idiomatic.  Also, on my
> machine, with a slightly newer version of MLton, the change slowed down the
> benchmark from what I sent you on June 10 by a factor of 6.  That's quite a lot.
> I would like to request changing the benchmark back to the more idiomatic and 6x
> faster (or whatever it is on your machine) version.  Thanks.

Hello again.  Henry figured out what was going on to cause the large discrepancy
between the two versions of sieve.  It had nothing to do with Word8Array vs bool
array.  It had everything to do with a bug in the original version of
sieve.mlton that caused it to only initialize the array once, instead of once
per loop.  With the bug fixed, the performance of the Word8Array and bool array
versions are nearly identical in MLton.  I would like to request that
sieve.mlton be changed to the following, which is essentially the same as what
you have now, except that it uses bool array instead of Word8Array.  Thanks
again.

--------------------------------------------------------------------------------

val size = 8193

val flags = Array.array (size, false)

fun init () =
   let
      fun loop i =
	 if i < size
	    then (Array.update (flags, i, true); loop (i + 1))
	 else ()
   in loop 2
   end

fun do_elts (i, count) =
  if i < size
     then
	if Array.sub (flags, i)
	   then
	      let
		 fun loop k = 
		    if k < size
		       then (Array.update (flags, k, false); loop (k + i))
		    else ()
	      in loop (i + i); do_elts(i + 1,count + 1)
	      end
	else do_elts (i + 1, count)
  else count

fun printl [] = print "\n" | printl(h::t) = ( print h ; printl t )
fun atoi s = case Int.fromString s of SOME num => num | NONE => 0

fun doit () = (init (); do_elts (2, 0))
   
fun repeat i =
   if i = 0
      then doit ()
   else (doit (); repeat (i - 1))

fun main(name, param_list) =  let
	val arg = hd(param_list @ ["1"]);
	val num = atoi arg
	val count = repeat (num - 1)
    in  printl ["Count: ", Int.toString count];
	OS.Process.success
    end

val _ = main( CommandLine.name(), CommandLine.arguments() );