From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q34MAhp1015689 for ; Thu, 5 Apr 2012 00:10:43 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqkBABrGfE/RVaG2kGdsb2JhbABFDqZPiFYBiG4IIgEBAQEJCQ0HFAQjggkBAQEDARICLAEbEgsBAwELBgULAxchIgERAQUBChIGExIQh2IFC5xcCowWgnGEVz+IdgEFC5BEBIhYjQuBEY0/PYNTVw X-IronPort-AV: E=Sophos;i="4.75,370,1330902000"; d="scan'208";a="152753292" Received: from mail-gx0-f182.google.com ([209.85.161.182]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 05 Apr 2012 00:10:38 +0200 Received: by ggnk4 with SMTP id k4so710040ggn.27 for ; Wed, 04 Apr 2012 15:10:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=kmbg1sNCwaB1sGW7ni2yOzijd4m52pivfUJNovzitQA=; b=NHNAX2ZNTcgjK5SA70flGQob52NQiDkPkSkCJvWq2OZ+iimujVZiYpBcFJh40fZQL7 rF0EvbO6BYZD6dHk68q9iS9RE6MYtGU7jJHzjk1VlL7FNlur2dzUgkHSYHrWtiIEsSdq MYvP/3fBOrOd1gBbqzLT+EvsUgmvBM5WV2t/tFPUx+1110ySYPG0qgiCf5p/DD5/0yvB SgrboZWRKtBikdOmZL2u8MNAlc2hgs8ksn1418AvnTBxePRLVBJZeKf2Woeaxxk61dOT HWr9VyyL8/I7zfej2mS8u0vBrE5eM6W0F6PIRFuMl4ZlTg6Rd0OQ+RUl4FT7ajzAgHoY hwNA== MIME-Version: 1.0 Received: by 10.60.170.145 with SMTP id am17mr50940oec.73.1333577437203; Wed, 04 Apr 2012 15:10:37 -0700 (PDT) Received: by 10.182.202.69 with HTTP; Wed, 4 Apr 2012 15:10:37 -0700 (PDT) In-Reply-To: <201204042038.q34KcPcj001730@outgoing.mit.edu> References: <201204042038.q34KcPcj001730@outgoing.mit.edu> Date: Wed, 4 Apr 2012 15:10:37 -0700 Message-ID: From: Julien Verlaguet To: John Carr Cc: Pierre Chopin , caml-list@inria.fr Content-Type: multipart/alternative; boundary=bcaec54c4fae6ccb9404bce1b1ad Subject: Re: [Caml-list] exn vs option --bcaec54c4fae6ccb9404bce1b1ad Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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_found is never caught, then using exceptions is faster than option because you don't have to allocate to perform the "find" operation (the difference should be minimal though). Plus it makes your code more readable, you don't have to match on the return value of the find. This use case makes sense if you don't intend to catch Not_found, when Not_found is a bug, period. Le 4 avril 2012 13:38, John Carr a =E9crit : > > 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 =3D 0 to 10000000 do try ... done > > executes "try" 10000001 times and will run much more slowly than > > try for i =3D 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 > > --bcaec54c4fae6ccb9404bce1b1ad Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
2 more use cases:

1) When writing a deep recu= rsion
Throwing an exception will directly jump to your "catc= h" 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&qu= ot;.

2) When your exception is exceptional :-)
If = Not_found is never caught, then using exceptions is faster than option beca= use you don't have to allocate to perform the "find" operatio= n (the difference should be minimal though).
Plus it makes your code more readable, you don't have to match on = the return value of the find.
This use case makes sense if you do= n't intend to catch Not_found, when Not_found is a bug, period.

Le 4 avril 2012 13:38, John Carr <jfc@mit.edu> a= =E9crit :

When thinking about performance, consider the "try" keyword to ta= ke time
to execute. =A0A try block pushes an exception handler onto a stack and
pops the stack on exit. =A0The try block may also interfere with tail call<= br> optimizations.

A loop like

=A0for i =3D 0 to 10000000 do try ... done

executes "try" 10000001 times and will run much more slowly than<= br>
=A0try for i =3D 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. =A0For 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<= br> 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 exc= eption
> that is caught, in the other the function returns an option that is pa= ttern
> matched on.
>
> I noticed that, whether the exception is thrown or not, the option ver= sion is
> always faster.
>
> Is there any case where it makes sense, performance wise, to use excep= tion
> instead of 'a option ?

--
Caml-list mailing list. =A0Subscription 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


--bcaec54c4fae6ccb9404bce1b1ad--