From: "Daniel M. Albro" <albro@humnet.ucla.edu>
To: caml-list@inria.fr
Subject: Re: [Caml-list] OCaml popularity
Date: 13 Mar 2003 21:49:07 -0800 [thread overview]
Message-ID: <1047620947.1866.21.camel@giynz> (raw)
In-Reply-To: <15985.1204.814698.939943@h00045a4799d6.ne.client2.attbi.com>
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
next prev parent reply other threads:[~2003-03-14 5:49 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-03-13 7:09 Daniel M. Albro
2003-03-13 16:48 ` Neel Krishnaswami
2003-03-13 21:29 ` Karl Zilles
2003-03-13 21:36 ` Daniel M. Albro
2003-03-13 21:42 ` Daniel M. Albro
[not found] ` <15985.1204.814698.939943@h00045a4799d6.ne.client2.attbi.com>
2003-03-14 5:49 ` Daniel M. Albro [this message]
2003-03-14 9:05 ` Ville-Pertti Keinonen
2003-03-14 9:13 ` Daniel M. Albro
2003-03-13 21:53 ` Brian Hurt
-- strict thread matches above, loose matches on Subject: below --
2003-03-15 16:27 Oliver Bandel
2003-03-15 17:55 ` Sergey Goldgaber
2003-03-14 22:14 Daniel M. Albro
2003-03-13 14:39 [oliver: Re: [Caml-list] OCaml popularity] Oliver Bandel
2003-03-13 16:35 ` [Caml-list] OCaml popularity Michael Schuerig
2003-03-12 23:53 [oliver: Re: [Caml-list] OCaml popularity] Oliver Bandel
2003-03-13 1:34 ` [Caml-list] OCaml popularity Michael Schuerig
2003-03-12 17:40 isaac gouy
2003-03-06 23:27 Graham Guttocks
2003-03-10 20:43 ` Paul Steckler
2003-03-10 23:48 ` Gerd Stolpmann
2003-03-11 0:18 ` Brian Hurt
2003-03-17 23:49 ` Graham Guttocks
2003-03-11 1:43 ` Nicolas Cannasse
2003-03-11 10:23 ` Pierre Weis
2003-03-11 14:27 ` Guillaume Marceau
2003-03-11 16:16 ` David Brown
2003-03-12 2:32 ` Nicolas Cannasse
2003-03-12 10:51 ` Alex Romadinoff
2003-03-12 18:24 ` Max Kirillov
2003-03-11 19:02 ` Graham Guttocks
2003-03-12 17:12 ` Richard W.M. Jones
2003-03-12 18:08 ` Alwyn Goodloe
2003-03-12 22:34 ` Michael Schuerig
2003-03-12 23:13 ` Martin Weber
2003-03-12 23:35 ` Michael Schuerig
2003-03-13 8:02 ` Alessandro Baretta
2003-03-13 10:23 ` Michael Schuerig
2003-03-12 23:35 ` Brian Hurt
2003-03-12 23:18 ` Daniel Bünzli
2003-03-12 23:47 ` Brian Hurt
2003-03-13 2:15 ` William Lovas
2003-03-13 3:44 ` Graham Guttocks
2003-03-13 9:31 ` Richard W.M. Jones
[not found] ` <20030313095232.GC347@first.in-berlin.de>
2003-03-13 20:50 ` William Lovas
2003-03-13 21:17 ` Oliver Bandel
2003-03-13 22:01 ` Brian Hurt
2003-03-13 22:17 ` Oliver Bandel
2003-03-14 6:33 ` Michal Moskal
2003-03-14 11:50 ` Markus Mottl
2003-03-14 15:38 ` Oliver Bandel
2003-03-14 10:13 ` MikhailFedotov
2003-03-14 10:30 ` Johann Spies
2003-03-13 8:09 ` Pierre Weis
2003-03-15 1:43 ` Tushar Samant
2003-03-15 8:19 ` Andreas Eder
2003-03-11 16:26 ` Fred Yankowski
2003-03-12 18:59 ` Martin Weber
2003-03-12 20:24 ` Xavier Leroy
2003-03-13 0:42 ` Graham Guttocks
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=1047620947.1866.21.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