caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Daniel M. Albro" <albro@humnet.ucla.edu>
To: caml-list@inria.fr
Subject: [Caml-list] Loop times
Date: 13 Mar 2003 13:53:59 -0800	[thread overview]
Message-ID: <1047592439.1866.10.camel@giynz> (raw)


	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


             reply	other threads:[~2003-03-13 21:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-13 21:53 Daniel M. Albro [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1047592439.1866.10.camel@giynz \
    --to=albro@humnet.ucla.edu \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).