Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] ocaml and large development projects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michal Moskal <malekith@p...>
Subject: Re: [Caml-list] Reading a file
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