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 WAA14247; Thu, 13 Mar 2003 22:54:51 +0100 (MET) 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 WAA14469 for ; Thu, 13 Mar 2003 22:54:50 +0100 (MET) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from giynz (vp191026.reshsg.uci.edu [128.195.191.26]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h2DLsjf20088 for ; Thu, 13 Mar 2003 22:54:48 +0100 (MET) Received: from albro by giynz with local (Exim 3.36 #1 (Debian)) id 18taeF-0001LX-00 for ; Thu, 13 Mar 2003 13:53:59 -0800 Subject: [Caml-list] Loop times From: "Daniel M. Albro" To: caml-list@inria.fr Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1047592439.1866.10.camel@giynz> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 Date: 13 Mar 2003 13:53:59 -0800 X-Spam: no; 0.00; ucla:99 incr:01 recursion:01 rec:01 exception:02 recursive:03 tail:03 sys:03 imperative:04 let:04 ary:04 raise:05 daniel:06 loop:06 break:11 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk OK, I just did a test of the three methods. Here's the code: Exception Version: ------------------------------------------------- exception Break let _ = let ary = [|1;2;3;4;5;6;7;8;9;10;11;12|] in for i = 1 to 1_000_000_000 do try for j = 1 to 10 do if ary.(j) = 5 then raise Break done with Break -> () done real 0m30.569s user 0m30.250s sys 0m0.140s ------------------------------------------------------ Straight imperative version: ------------------------------------------------------ let _ = let ary = [|1;2;3;4;5;6;7;8;9;10;11;12|] in let j = ref 0 in for i = 1 to 1_000_000_000 do j := 0; while !j < 10 do if ary.(!j) = 5 then j := 10; incr j done done real 0m40.498s user 0m39.980s sys 0m0.260s ------------------------------------------------------ Tail recursive version: ------------------------------------------------------ let _ = let ary = [|1;2;3;4;5;6;7;8;9;10;11;12|] in let rec loop j = if j = 10 then () else if ary.(j) = 5 then () else loop (j + 1) in for i = 1 to 1_000_000_000 do loop 1 done real 0m22.571s user 0m22.360s sys 0m0.080s ------------------------------------------------------- So may I should just be quiet and use recursion :). -- Daniel M. Albro ------------------- 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