caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Mattias Engdegård" <mattias@virtutech.se>
To: olivier.roussel@crans.org
Cc: yinso.chen@gmail.com, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] best and fastest way to read lines from a file?
Date: Tue, 2 Oct 2007 14:39:58 +0200	[thread overview]
Message-ID: <200710021239.l92CdwZ15641@virtutech.se> (raw)
In-Reply-To: <47016CEE.8010704@crans.org> (message from Olivier Roussel on Mon, 01 Oct 2007 23:55:58 +0200)

>let line_count filename =
>   let f = open_in filename in
>   let rec loop count =
>     match (readline f) with
>       | Some(_) -> loop (count+1)
>       | None -> count in
>   loop 0;;

Something like this should be even faster:

exception Done of int

let line_count file =
  let rec loop count =
    let _ =
      try
	input_line f
      with End_of_file -> raise (Done count)
    in
      loop (count + 1)
  in
    try loop 0 with Done x -> x

as it avoids unnecessary consing in the inner loop.


  reply	other threads:[~2007-10-02 12:40 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-01 21:27 YC
2007-10-01 21:55 ` [Caml-list] " Daniel Bünzli
2007-10-01 22:29   ` YC
2007-10-01 21:55 ` Olivier Roussel
2007-10-02 12:39   ` Mattias Engdegård [this message]
2007-10-02 12:56     ` Brian Hurt
2007-10-02 16:15       ` kirillkh
2007-10-02 17:10         ` verlyck, Bruno.Verlyck
2007-10-02 18:02         ` kirillkh
2007-10-02 19:35           ` skaller
2007-10-02 21:05             ` kirillkh
2007-10-02 21:07               ` Jon Harrop
2007-10-02 20:23           ` Olivier Andrieu
2007-10-02 20:49             ` kirillkh
2007-10-02 21:10               ` Jon Harrop
2007-10-02 21:15               ` David Allsopp
2007-10-02 22:23                 ` skaller

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=200710021239.l92CdwZ15641@virtutech.se \
    --to=mattias@virtutech.se \
    --cc=caml-list@yquem.inria.fr \
    --cc=olivier.roussel@crans.org \
    --cc=yinso.chen@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).