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
pie...@punchup.com

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to