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=SPF_PASS autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id CA45BBC69 for ; Mon, 1 Oct 2007 23:55:59 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAKoJAUeK54gDh2dsb2JhbACOMgIJCic X-IronPort-AV: E=Sophos;i="4.21,218,1188770400"; d="scan'208";a="2219825" Received: from rouge.crans.org ([138.231.136.3]) by mail2-smtp-roc.national.inria.fr with ESMTP; 01 Oct 2007 23:55:59 +0200 Received: from localhost (localhost.crans.org [127.0.0.1]) by rouge.crans.org (Postfix) with ESMTP id 76B8B810F; Mon, 1 Oct 2007 23:55:59 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at crans.org Received: from rouge.crans.org ([10.231.136.3]) by localhost (rouge.crans.org [10.231.136.3]) (amavisd-new, port 10024) with LMTP id dYGMnxPYZ2Pw; Mon, 1 Oct 2007 23:55:59 +0200 (CEST) Received: from [138.231.137.26] (merlin.crans.org [138.231.137.26]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by rouge.crans.org (Postfix) with ESMTP id 55071810B; Mon, 1 Oct 2007 23:55:59 +0200 (CEST) Message-ID: <47016CEE.8010704@crans.org> Date: Mon, 01 Oct 2007 23:55:58 +0200 From: Olivier Roussel User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 To: YC Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] best and fastest way to read lines from a file? References: <779bf2730710011427g5983da4cw6ad8b715a9e38771@mail.gmail.com> In-Reply-To: <779bf2730710011427g5983da4cw6ad8b715a9e38771@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; ocaml:01 printf:01 printf:01 recursive:01 readline:01 readline:01 crans:98 2.5:98 rec:01 rec:01 caml-list:01 exceptions:01 match:02 python:03 let:03 YC a écrit : > Hi all - Hi! > OCaml code: > (* test.ml *) > let rec line_count filename = > let f = open_in filename in > let rec loop file count = > try > ignore (input_line file); > loop file (count + 1) > with > End_of_file -> count > in > loop f 0;; > > let count = line_count <345k-line.txt> in > Printf.printf "Done: %d" count;; The following solution is ~2.5x faster than the Python implementation on my computer. Because there is no more exceptions in recursive calls, and thanks to tail-recursion. let readline f = try Some (input_line f) with End_of_file -> None;; 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;; let count = line_count <345k-line.txt> in Printf.printf "Done: %d\n" count;; -- Olivier Roussel