Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <xavier.leroy@inria.fr>
To: Christian Lindig <lindig@eecs.harvard.edu>,
	oliver@first.in-berlin.de, Caml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Performance-cost of ^
Date: Sun, 30 Mar 2003 12:20:48 +0200	[thread overview]
Message-ID: <20030330122048.C22539@pauillac.inria.fr> (raw)
In-Reply-To: <20030328162110.GB548@eecs.harvard.edu>; from lindig@eecs.harvard.edu on Fri, Mar 28, 2003 at 05:21:10PM +0100

> > 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
String.concat.

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

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


      reply	other threads:[~2003-03-30 10:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-26  9:52 Oliver Bandel
2003-03-26 10:03 ` Basile STARYNKEVITCH
2003-03-26 12:28 ` Damien Doligez
2003-03-28 16:21   ` Christian Lindig
2003-03-30 10:20     ` Xavier Leroy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030330122048.C22539@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=lindig@eecs.harvard.edu \
    --cc=oliver@first.in-berlin.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox