caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] exn vs option
@ 2012-04-04 20:25 Pierre Chopin
  2012-04-04 20:38 ` John Carr
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Pierre Chopin @ 2012-04-04 20:25 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1030 bytes --]

Hi,

I benchmarked two programs, in one case the main function throw an
exception that is caught, in the other the function returns an option that
is pattern matched on.

I noticed that, whether the exception is thrown or not, the option version
is always faster.

Is there any case where it makes sense, performance wise, to use exception
instead of 'a option ?

test1.ml
----------------------------------------------------------------------

exception Foo
let f x =
 if x =1 then raise Foo else ()

;;

 for i = 0 to 10_000_000 do
try
    f 1
with Foo -> ()
done
------------------------------------------------------------------------
test2.ml:
------------------------------------------------------------------------
let f x =
    if x=1 then None else Some ()

;;
for i = 0 to 10_000_000 do
    match f 1 with
        None -> ()
    |   Some s -> s
    done
------------------------------------------------------------------------



-- 
Pierre Chopin,
Chief Technology Officer and co-founder
punchup LLC
pierre@punchup.com

[-- Attachment #2: Type: text/html, Size: 1690 bytes --]

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

end of thread, other threads:[~2012-04-11 10:36 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-04 20:25 [Caml-list] exn vs option Pierre Chopin
2012-04-04 20:38 ` John Carr
2012-04-04 22:10   ` Julien Verlaguet
2012-04-05  1:29     ` Francois Berenger
2012-04-05  6:45 ` Raphael Proust
2012-04-05  7:53   ` Benedikt Grundmann
2012-04-05  9:05 ` Goswin von Brederlow
2012-04-05  9:50   ` Daniel Bünzli
2012-04-11 10:26     ` Goswin von Brederlow
2012-04-11 10:32       ` David House
2012-04-11 10:36         ` David House
2012-04-05 20:19   ` Pierre Chopin

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