2007/10/3, Daniel de Rauglaudre <daniel.de_rauglaudre@inria.fr>:
Hi,

On Wed, Oct 03, 2007 at 02:19:56PM +0200, kirillkh wrote:

> But then someone suggested using a second exception instead, which
> is better performance-wise [...]

Is that been checked ? And the two implementations tested ? What are the
results, in time ?

Tested from the top-level on a text file with 11mln lines:
variants:
8.081  8.021  8.072  8.052  8.011  =>  avg=8.0474
exceptions:
7.801  7.902  7.822  7.901  7.832  =>  avg=7.8512
-----------------------------
total: exceptions are 2.44% faster

I'm having troubles with ocamlopt (windows machine), can anyone do a similar test with it? Here's the code used (it's the pre-combinator version of line counter):

exceptions (lcex.ml):
exception Done of int;;

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

let start_time = Sys.time() in
let count = line_count "c:/kirill/ocaml/test3.txt" in
let diff = Sys.time() -. start_time in
    Printf.printf "count: %d,  time: %f" count diff;;

variants(lcvar.ml):
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 start_time = Sys.time() in
let count = line_count "c:/kirill/ocaml/test3.txt" in
let diff = Sys.time() -. start_time in
    Printf.printf "count: %d,  time: %f" count diff;;


-Kirill