From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA01848; Wed, 10 Jul 2002 12:17:25 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA01840 for ; Wed, 10 Jul 2002 12:17:23 +0200 (MET DST) Received: from mailhost.tni.fr (firewall.tni.fr [195.25.255.61]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g6AA3Rn17443 for ; Wed, 10 Jul 2002 12:03:27 +0200 (MET DST) Received: from groscool.tni.fr ([195.25.255.1]) by mailhost.tni.fr (Netscape Messaging Server 3.62) with SMTP id 595; Wed, 10 Jul 2002 12:03:07 +0200 Received: from 192.168.7.72 by groscool.tni.fr (InterScan E-Mail VirusWall NT); Wed, 10 Jul 2002 12:03:05 +0200 (Paris, Madrid (heure d'été)) Message-ID: <3D2C063E.25EA1D8D@tni.fr> Date: Wed, 10 Jul 2002 12:02:38 +0200 From: "sebastien FURIC" X-Mailer: Mozilla 4.7 [fr] (WinNT; I) X-Accept-Language: fr MIME-Version: 1.0 To: Shannon --jj Behrens CC: caml-list@inria.fr Subject: Re: [Caml-list] productivity improvement References: <20020709182022.69599.qmail@web10706.mail.yahoo.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hello, I've tested your O'Caml program on my PC: time ./findprimes_modcount 10000 real 1m26.089s user 0m0.010s sys 0m0.040s To my surprise, this naive lazy version in Clean (5 lines of code!) outperforms your version: module sieve import StdEnv primes =: sieve [2, 3 ..] where sieve [p : xs] = [p : sieve [x \\ x <- xs | x mod p <> 0]] Start = take 10000 primes time ./sieve real 1m17.460s user 0m0.010s sys 0m0.020s The corresponding version in O'Caml (using a lazy list module) is far from being as efficient: type 'a stream = Empty | Cons of 'a * 'a stream Lazy.t let rec iter f = function | Empty -> () | Cons (x, xs) -> f x; iter f (Lazy.force xs) let rec filter p = function | Empty -> Empty | Cons (x, xs) -> if (p x) then Cons (x, lazy (filter p (Lazy.force xs))) else filter p (Lazy.force xs) let rec take n s = match (n, s) with | _, Empty -> Empty | 0, _ -> Empty | n, Cons (x, xs) -> Cons (x, lazy (take (n - 1) (Lazy.force xs))) let rec from n = Cons (n, lazy (from (n + 1))) let print_int_stream = iter (function x -> print_int x; print_string "; ") let primes = let rec sieve = function | Empty -> Empty | Cons (p, xs) -> Cons (p, lazy (sieve (filter (function n -> n mod p <> 0) (Lazy.force xs)))) in sieve (from 2);; print_int_stream (take 10000 primes) real 11m9.021s user 0m0.020s sys 0m0.030s Sebastien. ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners