Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] OCaml popularity
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Daniel M. Albro <albro@h...>
Subject: Re: [Caml-list] OCaml popularity

	I imagine everyone was laughing at me when they saw the timing
tests I did -- they weren't doing the same thing!  D'oh.  Here they are
again with comparable code (the conclusion is basically the same,
though -- use tail recursive functions if you want speed in OCaml,
at least if you're going to be exiting the loop early):

Break simulated with exceptions:
----------------------------------------------
exception Break

let _ =
  let ary = [|1;2;3;4;5;6;7;8;9;10;11;12|] in
    for i = 1 to 1_000_000_000 do
      try
        for j = 0 to 9 do
          if ary.(j) = 5 then
            raise Break
        done
      with Break -> ()
    done

real    0m35.123s
user    0m34.600s
sys     0m0.120s
-----------------------------------------------

Break by causing the while test to fail
-----------------------------------------------
let _ =
  let ary = [|1;2;3;4;5;6;7;8;9;10;11;12|] in
  let j = ref 0 in
    for i = 1 to 1_000_000_000 do
      j := 0;
      while !j < 10 do
        if ary.(!j) = 5 then
          j := 10
        else
          incr j
      done
    done

real    0m40.135s
user    0m39.600s
sys     0m0.200s
------------------------------------------------

Break simulated by tail recursive functions
------------------------------------------------
let _ =
  let ary = [|1;2;3;4;5;6;7;8;9;10;11;12|] in
  let rec loop j =
    if j = 10 then 
      ()
    else if ary.(j) = 5 then
      ()
    else
      loop (j + 1)
  in
    for i = 1 to 1_000_000_000 do
      loop 0
    done

real    0m27.075s
user    0m26.940s
sys     0m0.120s
------------------------------------------------

Break simulated by continuation passing (I think that's
what this is -- it looks like a call/cc to me, anyway)
------------------------------------------------
let escape body =
  let module Fail = struct exception T end in
  let datum = ref None in
  let throw v =
    begin
      datum := Some v;
      raise Fail.T
    end
  in
    try
      body throw
    with
        Fail.T -> (match !datum with Some v -> v | None -> assert false)


let _ =
  let ary = [|1;2;3;4;5;6;7;8;9;10;11;12|] in
    for i = 1 to 1_000_000_000 do
      escape (fun exit ->
                for j = 0 to 9 do
                  if ary.(j) = 5 then exit()
                done)
    done

real    1m50.006s
user    1m49.470s
sys     0m0.350s
------------------------------------------------


On Thu, 2003-03-13 at 14:22, Neel Krishnaswami wrote:
> Daniel M. Albro writes:
> > 
> > Hmm...  Apparently I'm a liar.  OK, I take it back.  Doing your
> > version 1 billion times takes 25 seconds on my machine, and my
> > version takes 29 seconds.  I still think a break statement would
> > look nicer.
> 
> Use the power of higher order functions!
> 
> let escape body =
>   let module Fail = struct exception T end in
>   let datum = ref None in
>   let throw v =
>     begin
>       datum := Some v;
>       raise Fail.T
>     end
>   in
>   try
>     body throw
>   with
>     Fail.T -> (match !datum with Some v -> v | None -> assert false)
> 
> Now you can bail out of any computations early, by using an escape:
> 
> escape (fun exit ->
>   for i = 1 to 100 do
>     Printf.printf ".";
>     if i = 25 then exit();
>   done)
> 
> What's nice about this is that it gives you a multi-level break, and
> that you can return values from it, too. Suppose you want to multiply
> the numbers in a list; you can use an escape to stop computing if we
> ever see a 0 in the list.
> 
> let multiply_ints lst =
>   escape (fun exit -> 
>     List.fold_left
>       (fun acc n -> if n = 0 then exit 0 else n * acc)
>       1 
>       lst)
-- 
Daniel M. Albro <albro@humnet.ucla.edu>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners