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 KAA17742; Wed, 21 May 2003 10:24:06 +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 KAA17724 for ; Wed, 21 May 2003 10:24:05 +0200 (MET DST) Received: from ep09.kernel.pl (ep09.kernel.pl [212.87.11.162]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h4L8O3H19192 for ; Wed, 21 May 2003 10:24:03 +0200 (MET DST) Received: (qmail 5556 invoked by uid 566); 21 May 2003 08:24:02 -0000 Date: Wed, 21 May 2003 10:21:22 +0200 From: Michal Moskal To: Siegfried Gonzi Cc: caml-list@inria.fr Subject: Re: [Caml-list] Reading a file Message-ID: <20030521082122.GA3696@roke.freak> References: <4.3.2.7.2.20030517225010.04b748a0@localhost> <4.3.2.7.2.20030519120753.04545700@localhost> <200305201007.17990.wolfgang.mueller2@uni-bayreuth.de> <3EC9EA84.3070404@stud.uni-graz.at> <20030520132032.GA9564@roke.freak> <3ECB189C.5090400@stud.uni-graz.at> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3ECB189C.5090400@stud.uni-graz.at> User-Agent: Mutt/1.4.1i X-PGP-Fingerprint: CF89 1B14 11BE 1CC9 2CA3 7497 5E32 69B4 BC71 B4C2 X-AntiVirus: scanned for viruses by AMaViS 0.2.1 (http://amavis.org/) X-Spam: no; 0.00; michal:01 moskal:01 malekith:01 pld-linux:01 caml-list:01 siegfried:01 gonzi:01 excessively:01 floats:01 wroclaw:01 arrays:01 kernel:01 rec:01 0200,:01 match:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, May 21, 2003 at 08:11:40AM +0200, Siegfried Gonzi wrote: > let split s c = > let rec loop start acc = > try > let next = String.index_from s start c in > let substring = String.sub s start (next-start) in > loop (next+1) (substring :: acc) > with > Not_found -> > let len = String.length s in > let substring = String.sub s start (len-start) in > List.rev (substring :: acc) > in loop 0 [] > ;; > > > let frob userval s = > match s with > | "n/a" -> userval > | "nil" -> userval > | _ -> float_of_string s > ;; > > let extractFloats file del nanProxy = > let rec readLoop i acc = > try > let line = input_line file in > let floatL = List.map (frob nanProxy) (split line del) in > readLoop (i+1) (floatL :: acc) > with > End_of_file -> > List.rev acc > in > readLoop 0 [] > ;; This function is *not* tail recursive. You should try something like this: let try_input file = try Some (input_line file) with None -> None let extractFloats file del nanProxy = let rec readLoop i acc = match try_input file with | Some line -> let floatL = List.map (frob nanProxy) (split line del) in readLoop (i+1) (floatL :: acc) | None -> List.rev acc in readLoop 0 [] This alone makes your program 2 times faster (maybe more for you data). split (and List.map for that matter) function also ain't tail recursive, but this shouldn't be the case if your lines aren't excessively long (say over 1000 floats). Next you should note that float lists are boxed, maybe use arrays? Hope that helps. -- : Michal Moskal :: http://www.kernel.pl/~malekith : GCS {C,UL}++++$ a? !tv : PLD Linux ::::::::: Wroclaw University, CS Dept : {E-,w}-- {b++,e}>+++ h ------------------- 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