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
best and fastest way to read lines from a file?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-10-02 (17:10)
Subject: Re: [Caml-list] best and fastest way to read lines from a file?
   Date: Tue, 2 Oct 2007 18:15:57 +0200
   From: kirillkh <kirillkh@gmail.com>
   > This should be a FAQ.
   Since we're talking of 10+ lines of code and only one case among
   many possible (you might also want to do something fairly similar,
   but not quite the same, as iterating over all words or characters
   in a file, doing something else than counting, etc.), I would
   rather see it implemented in a library as combinator.  What I have
   in mind is a function that goes over a file and invokes some user
   code on each block of bytes/characters/lines/words/... The points
   of customization would be:
   * how to detect the start and end of block
   * routine to pass the blocks to

   Then, on top of this combinator, build block-specific ones: for
   byte, char, line, word blocks.  Also make it possible to customize
   buffering behavior.

   Being new to OCaml, I'm interested in comments; is what I suggest a
   good idea?
Yes, why not ?

   If yes, why hasn't anyone implemented it yet?  
I believe Cash (in the Hump:
has some of the things you ask for: look around fold_in_channel (as a
combinator; yes, it *is* 5 lines of code), and for what you call
blocks, chapters 6 & 7 of the documentation (Reading delimited strings
& Record I/O and field parsing).  Buffering is also parameterizable
(between 1 and 4Kb, no line buffering, sorry, too much C code to
modify in the Ocaml runtime).

It may not suit your taste, but when generalizing, everybody tends to
have one's own very specific idea of how to do it.  Human nature...
At least Cash can give you some ideas.

Of course, the OP was asking for the fastest way...  OK, we aren't


Disclaimer: Cash is still not ported to Ocaml 3.10; but 3.09 is fine.
Have to choose: camlp4 or 5... ?