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 10000000 do try ... done

executes "try" 10000001 times and will run much more slowly than

  try for i = 0 to 10000000 do ... done

where "try" only executes once.

I use options where performance matters, in frequently executed code
where the amount of computation is not much more than the overhead of
a try...with.  For example, I have variants of List.assoc that return
options instead of raising exceptions.

Where performance doesn't matter, i.e. the amount of code in the block
is large or the block is rarely executed, I use exceptions or options
based on convenience.

> 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 ?

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