From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id BD535BC6C for ; Tue, 2 Oct 2007 14:40:00 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ah4FAMLYAUc+FFrD/2dsb2JhbACBWQ X-IronPort-AV: E=Sophos;i="4.21,220,1188770400"; d="scan'208";a="3650725" Received: from vtab.com (HELO oden.vtab.com) ([62.20.90.195]) by mail3-smtp-sop.national.inria.fr with ESMTP; 02 Oct 2007 14:40:00 +0200 Received: from oden.vtab.com (oden.vtab.com [127.0.0.1]) by oden.vtab.com (Postfix) with ESMTP id 12EFD26EF0D; Tue, 2 Oct 2007 14:39:59 +0200 (CEST) Received: from virtutech.se (alfredo.hq.vtech [10.0.0.52]) by oden.vtab.com (Postfix) with ESMTP id E85F926EE65; Tue, 2 Oct 2007 14:39:58 +0200 (CEST) Received: (from mattias@localhost) by virtutech.se (8.11.6/8.11.6) id l92CdwZ15641; Tue, 2 Oct 2007 14:39:58 +0200 Date: Tue, 2 Oct 2007 14:39:58 +0200 Message-Id: <200710021239.l92CdwZ15641@virtutech.se> From: "=?utf-8?b?TWF0dGlhcyBFbmdkZWfDpQ==?= =?utf-8?b?cmQ=?=" To: olivier.roussel@crans.org Cc: yinso.chen@gmail.com, caml-list@yquem.inria.fr In-reply-to: <47016CEE.8010704@crans.org> (message from Olivier Roussel on Mon, 01 Oct 2007 23:55:58 +0200) Subject: Re: [Caml-list] best and fastest way to read lines from a file? Content-Type: text/plain; charset=iso-8859-1 References: <779bf2730710011427g5983da4cw6ad8b715a9e38771@mail.gmail.com> <47016CEE.8010704@crans.org> X-Virus-Scanned: ClamAV using ClamSMTP X-Spam: no; 0.00; mattias:01 mattias:01 readline:01 rec:01 rec:01 avoids:01 exception:01 caml-list:01 int:01 match:02 let:03 let:03 raise:03 file:11 file:11 >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.