caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Loop times
@ 2003-03-13 21:53 Daniel M. Albro
  2003-03-17 11:39 ` Fabrice Le Fessant
  0 siblings, 1 reply; 9+ messages in thread
From: Daniel M. Albro @ 2003-03-13 21:53 UTC (permalink / raw)
  To: caml-list


	OK, I just did a test of the three methods.  Here's the code:

Exception Version:
-------------------------------------------------
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 = 1 to 10 do
          if ary.(j) = 5 then
            raise Break
        done
      with Break -> ()
    done

real    0m30.569s
user    0m30.250s
sys     0m0.140s
------------------------------------------------------

Straight imperative version:
------------------------------------------------------
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;
        incr j
      done
    done

real    0m40.498s
user    0m39.980s
sys     0m0.260s
------------------------------------------------------

Tail recursive version:
------------------------------------------------------
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 1
    done

real    0m22.571s
user    0m22.360s
sys     0m0.080s
-------------------------------------------------------

	So may I should just be quiet and use
recursion :).

-- 
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


^ permalink raw reply	[flat|nested] 9+ messages in thread
* Re: [Caml-list] Loop times
@ 2003-03-18  8:52 Oliver Bandel
  2003-03-18 15:21 ` Florian Hars
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Oliver Bandel @ 2003-03-18  8:52 UTC (permalink / raw)
  To: caml-list

On Mon, Mar 17, 2003 at 02:01:21PM -0800, Daniel M. Albro wrote:
> 
> 	Sorry, I just meant that the version that puts the
> exception into a variable outside of the loop is a win over
> the one that creates the exception inside the loop.

What does this mean for such a version of reading a file
linewise into a list of strings?


let input_lines chan =
  let rec input_lines_helper res =
    let sl = 
       try
         Some (input_line chan) 
       with
         End_of_file -> None in
    match sl with
       None -> List.rev res
     | Some l -> input_lines_helper (l :: res) in
  input_lines_helper []

There is a try-with inside the reacursive function.
But is there a way to avoid it?


Well... I may re-read all the mails and put them together to
one mail or may produce a little paper on that topic?

I have to sort the many new things in FPL-programming
for better understanding....

Or maybe you are interested in collecting all results together
into one conclusion-mail?
(Would be nice. :))


> The
> fastest loop routine overall was the tail recursive loop,
> i.e. the functional/recursive.

BTW: This is new to me. Even OCaml-people told me, that
the imperative version of loops will be faster than
recursive/functional.

That's good news for FP-programming, but that's also 
bad news for people, who want to optimize their
functional programs in speed.


> However, this latest
> imperative version has timing that's very close -- the
> imperative version that pre-builds the exception takes
> just over 28 seconds, and the tail-recursive version
> takes just under 27 seconds.

OK.

Can you give me a short advice on the recursive
Input-function, mantioned above?

Thanks.


Ciao,
   Oliver

-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2003-03-19  6:49 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-13 21:53 [Caml-list] Loop times Daniel M. Albro
2003-03-17 11:39 ` Fabrice Le Fessant
2003-03-17 18:59   ` Daniel M. Albro
     [not found]     ` <20030317214841.GA467@first.in-berlin.de>
2003-03-17 22:01       ` Daniel M. Albro
2003-03-18  9:57         ` Oliver Bandel
2003-03-18  8:52 Oliver Bandel
2003-03-18 15:21 ` Florian Hars
2003-03-18 15:40 ` Noel Welsh
2003-03-19  6:48 ` Daniel M. Albro

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).