English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Performance-cost of ^
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-03-30 (10:20)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] Performance-cost of ^
> > Oliver Bandel wrote:
> > >I'm reading in a file linewise.  For some operations I need it as one
> > >long string.
> > >
> > >How to acchieve this performant?  Is it ok to use ^ for a list of
> > >lines (with  List.fold_right? or List.fold_left?)

It's inefficient for large files (quadratic time).

Christian Linding wrote:
> Damien Doligez wrote:
> > You should use String.concat.
> What about using the Buffer module? It sounds like it was especially
> designed to build up long strings.

Yes, Buffer will work fine here, with about the same efficiency as

If the file is a regular file and isn't expected to change during
reading, the simplest and most efficient solution is:

   let ic = open_in_bin filename in
   let len = in_channel_length ic in
   let s = String.create len in
   really_input ic s 0 len;
   close_in ic;

In other circumstances (i.e. reading from a pipe or socket; desire to
read the file as a text file and not a binary file), consider the
following solution:

  let b = Buffer.create 1024 in
  let s = Buffer.create 1024 in
  let rec read_channel ic =
    let n = input ic s 0 1024 in
    if n > 0 then begin Buffer.add_substring b s 0 n; read_channel ic end 
    read_channel ic; Buffer.contents b

Many ways to skin a cat, I'm afraid.

- Xavier Leroy

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