Aside from performance considerations, there are semantics differences to take into account. This blog post explain why exceptions are "better" (or, more precisely, why it is not generally a good idea to replace exceptions by options) http://blog.dbpatterson.com/post/9528836599 (it is in Haskell rather than OCaml, but it still applies).
On Wed, Apr 4, 2012 at 10:25 PM, Pierre Chopin <pie...@punchup.com> wrote: > 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 > -- _______ Raphael -- 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