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: Mattias Waldau <mattias.waldau@a...>
Subject: RE: [Caml-list] Reading a file
If the files are not of megabyte size, I just read 
them into a string. Very fast.

(** return the contents of filename as a string *)
let read_as_string filename =
  (* read at most len chars into string s, return the number of chars
read *)
  let rec my_input ic s ofs len =
    if len <= 0 then ofs else begin
      let r = input ic s ofs len in
      if r = 0
      then ofs
      else my_input ic s (ofs+r) (len-r)
    end in
  let ic = open_in_bin filename in
  let max_size = in_channel_length ic in
  let buf = String.create max_size in
  let read_chars = my_input ic buf 0 max_size in
  close_in ic;
  String.sub buf 0 read_chars ;;

> -----Original Message-----
> From: owner-caml-list@pauillac.inria.fr 
> [mailto:owner-caml-list@pauillac.inria.fr] On Behalf Of 
> Siegfried Gonzi
> Sent: den 20 maj 2003 10:43
> To: 'caml-list@inria.fr'
> Subject: [Caml-list] Reading a file
> 
> 
> Hi:
> 
> Is there a better way in Ocaml to read a file line by line 
> than via the 
> read_line function?
> 
> I use read_line on a file, perform some tasks on this line 
> and store the 
> results in a list and after having red the file I use List.rev. The 
> problem actually is on big files the function is awfully slow. As 
> similar Clean function takes 15 seconds, my Bigloo program takes 25 
> second and my C++ programs (via templates) takes 25 secondes but my 
> Ocaml program takes 8 minutes.
> 
> I am not sure how quick List.rev actually is? In Bigloo 
> reversing a list 
> has more or less no overhead. My Bigloo function is similar 
> to my OCaml 
> function. Could it be that OCaml is that slow because I use "try and 
> with" constructs in order to check for the end of a file?
> 
> Why my Clean function is that fast is incomprehensible for 
> me. Does one 
> know whether there exists a function in OCaml which converts 
> a String to 
> a character-list? I use this construct in Clean then in order 
> to extract 
> floating point numbers from that character list: 
> ['1','.','2',...] and 
> store this floating point numbers via pattern matching in my 
> result-list.
> 
> S. Gonzi
> 
> 
> >
> >
> 
> 
> 
> -------------------
> 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

-------------------
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