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 MAA32284; Thu, 22 May 2003 12:26:59 +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 MAA32201 for ; Thu, 22 May 2003 12:26:55 +0200 (MET DST) Received: from fichte.ai.univie.ac.at (fichte.ai.univie.ac.at [131.130.174.156]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h4MAQtH19121 for ; Thu, 22 May 2003 12:26:55 +0200 (MET DST) Received: from fichte.ai.univie.ac.at (markus@localhost.ai.univie.ac.at [127.0.0.1]) by fichte.ai.univie.ac.at (8.12.3/8.12.3/Debian-6.3) with ESMTP id h4MAQswf023879; Thu, 22 May 2003 12:26:54 +0200 Received: (from markus@localhost) by fichte.ai.univie.ac.at (8.12.3/8.12.3/Debian-6.3) id h4MAQsOV023878; Thu, 22 May 2003 12:26:54 +0200 Date: Thu, 22 May 2003 12:26:54 +0200 From: Markus Mottl To: Siegfried Gonzi Cc: caml-list@inria.fr Subject: Re: [Caml-list] Reading a file Message-ID: <20030522102654.GA23001@fichte.ai.univie.ac.at> Mail-Followup-To: Siegfried Gonzi , caml-list@inria.fr 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> <20030521091120.GA9455@fichte.ai.univie.ac.at> <3ECC6DD5.9050509@stud.uni-graz.at> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3ECC6DD5.9050509@stud.uni-graz.at> User-Agent: Mutt/1.4.1i X-Spam: no; 0.00; caml-list:01 siegfried:01 gonzi:01 donnerstag:01 bigloo:01 shorter:01 floats:01 erg:01 arrays:01 regexp:01 inefficient:01 ocaml:01 schrieb:01 mottl:02 float:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Siegfried Gonzi schrieb am Donnerstag, den 22. Mai 2003: > Thanks for this version. Without any insult, it works well and takes for > this legendary file now 25 seconds, though, I find the version ugly. The > Clean, Bigloo, Fortran 95 and C++ version more or less takes the same time. There is always room for making things faster or shorter: it depends on what you prefer. If you want to have an even faster version, you could use float arrays instead of float lists. E.g. instead of this line in the previous solution: | Some line -> loop (List.map my_frob (split line del) :: acc) Use this: | Some line -> let str_ar = Array.of_list (split line del) in loop (Array.map my_frob str_ar :: acc) This is probably what you really need anyway. If you want to have a short version, which is still pretty fast, use this: --------------------------------------------------------------------------- let frob userval = function | "n/a" | "nil" -> userval | s -> float_of_string s ;; let extract_floats file del nan_proxy = let rex = Str.regexp_string (String.make 1 del) in let my_frob = frob nan_proxy in let acc_ref = ref [] in (try while true do let line = input_line file in let str_ar = Array.of_list (Str.split rex line) in acc_ref := Array.map my_frob str_ar :: !acc_ref done with End_of_file -> ()); List.rev !acc_ref ;; let f = open_in "/home/gonzi/test.txt";; let erg2 = extract_floats f ',' (-1.0);; let rows = List.length erg2;; rows;; --------------------------------------------------------------------------- > But there remains my requirement for a better OCaml solution. Better = faster or smaller...? > 1) Convert the string-line to a character-list: [x\\x<-: string-line] This would be _very_ inefficient. > This, oh dear Watson is what I mean with elegance. No need of string > indexing counting or other error prone stuff. Use regular expressions: fast enough and very short. > So, this must also be possible in OCaml - or must not? You cannot just take one solution of one language and assume that it is equally efficient or elegant in another language. This also heavily depends on availability and functionality of libraries. E.g. there is no "take_while" or "drop_while" in the OCaml standard library - you'd have to write them yourself. Regards, Markus Mottl -- Markus Mottl http://www.oefai.at/~markus markus@oefai.at ------------------- 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