Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: -- (:)
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 Archives:
Bug reports: FAQ:
Beginner's list: