[Shootout-list] alternative Oz prodcons

Einar Karttunen ekarttun@cs.helsinki.fi
Fri, 25 Jun 2004 11:29:03 +0300


Hello

This is much faster than the current solution and 
shorter too... Is this kind of "cheating" allowed?
Oz internally manages the synchronisation etc. 

Startup overhead of oz (including argument parsing),
is quite visible for N=100000, but it is remedied
when N>=1000000.

- Einar Karttunen

functor 
import System Application
define
   proc {Producer N Xs}
      case Xs of X|Xr then X = N {Producer N+1 Xr}
      [] nil then skip
      end 
   end 
   proc {Consumer N Xs}
      if N=<0 then Xs=nil 
      else X|Xr = Xs in {Consumer N-1 Xr}
      end 
   end
   N Args
in 
   [Args] = {Application.getArgs plain}
   N = {String.toInt Args}
   {Consumer N thread {Producer 0 $} end}
   {System.showInfo N#' '#N}
   {Application.exit 0}
end