[Caml-list] exn vs option

2012-04-04 Thread Pierre Chopin
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 s

Re: [Caml-list] exn vs option

2012-04-04 Thread John Carr
When thinking about performance, consider the "try" keyword to take time to execute. A try block pushes an exception handler onto a stack and pops the stack on exit. The try block may also interfere with tail call optimizations. A loop like for i = 0 to 1000 do try ... done executes "tr

Re: [Caml-list] exn vs option

2012-04-04 Thread Julien Verlaguet
2 more use cases: 1) When writing a deep recursion Throwing an exception will directly jump to your "catch" block without having to unfold every return call site on the stack one by one. Which can be much faster if the recursion is very "deep". 2) When your exception is exceptional :-) If Not_fou

Re: [Caml-list] exn vs option

2012-04-04 Thread Francois Berenger
I think there was an article about options versus exceptions in the OCaml journal. If I remember well, the exceptions were faster, but I can't find back the exact benchmark and context of this assertion. Regards, F. On 04/05/2012 07:10 AM, Julien Verlaguet wrote: 2 more use cases: 1) When wri

Re: [Caml-list] exn vs option

2012-04-04 Thread Raphael Proust
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 ra

Re: [Caml-list] exn vs option

2012-04-05 Thread Benedikt Grundmann
That post is against maybe (aka option) as an error type as it looses information about the error. If you use an Either type such as Core's Result.t that argument is invalidated. In fact at Jane Street we try avoid the use of exceptions as they don't force the client of the library to handle the

Re: [Caml-list] exn vs option

2012-04-05 Thread Goswin von Brederlow
Pierre Chopin writes: > 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 faste

Re: [Caml-list] exn vs option

2012-04-05 Thread Daniel Bünzli
Le jeudi, 5 avril 2012 à 11:05, Goswin von Brederlow a écrit : > If you are writing a module then consider providing both flavours for > functions, one with exceptions and one with options. Even if you only do > something like this: I don't think it's a rule that should be applied consistently, I

Re: [Caml-list] exn vs option

2012-04-05 Thread Pierre Chopin
Thank you for your answers. I am actually in the case of a webserver where I need to avoid interrupting it, even in 'exceptional' cases, but rather report the problem in a log, and return a default value whenever it is possible. Options seem to be the best choice, not only from a safety point of vi

Re: [Caml-list] exn vs option

2012-04-11 Thread Goswin von Brederlow
Daniel Bünzli writes: > Le jeudi, 5 avril 2012 à 11:05, Goswin von Brederlow a écrit : >> If you are writing a module then consider providing both flavours for >> functions, one with exceptions and one with options. Even if you only do >> something like this: > > > I don't think it's a rule th

Re: [Caml-list] exn vs option

2012-04-11 Thread David House
On Wed, Apr 11, 2012 at 11:26 AM, Goswin von Brederlow wrote: >> However in the particular case of finding something in a data structure >> where the user could be confronted with a situation where he can prove that >> the datum will be found I think it's justified to provide both flavours. For

Re: [Caml-list] exn vs option

2012-04-11 Thread David House
On Wed, Apr 11, 2012 at 11:32 AM, David House wrote: > On Wed, Apr 11, 2012 at 11:26 AM, Goswin von Brederlow > wrote: >>> However in the particular case of finding something in a data structure >>> where the user could be confronted with a situation where he can prove that >>> the datum will b