From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q351TOZT023571 for ; Thu, 5 Apr 2012 03:29:27 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aq4CACn1fE+GoCGhmWdsb2JhbABFgxGjWJFwAQEBAQEICwsHFCeCCQEBBTIBBTYKEQsYCQQSCAcJAwIBAgEzARETBgIBAQ6Hdwu6Ho0rgyQEiFaNDYERhF+NNg X-IronPort-AV: E=Sophos;i="4.75,372,1330902000"; d="scan'208";a="139116603" Received: from postman1.riken.jp (HELO postman.riken.jp) ([134.160.33.161]) by mail4-smtp-sop.national.inria.fr with ESMTP; 05 Apr 2012 03:29:25 +0200 Received: from postman.riken.jp (postman1.riken.jp [127.0.0.1]) by postman.riken.jp (Postfix) with SMTP id 214A032C0272 for ; Thu, 5 Apr 2012 10:29:22 +0900 (JST) Received: from [172.27.98.103] (rikad98.riken.jp [134.160.214.98]) by postman.riken.jp (Postfix) with ESMTPA id 0D13932A008C for ; Thu, 5 Apr 2012 10:29:22 +0900 (JST) Message-ID: <4F7CF571.9030706@riken.jp> Date: Thu, 05 Apr 2012 10:29:21 +0900 From: Francois Berenger User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.28) Gecko/20120313 Thunderbird/3.1.20 MIME-Version: 1.0 To: caml-list@inria.fr References: <201204042038.q34KcPcj001730@outgoing.mit.edu> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-PMX-Version: 5.6.0.2009776, Antispam-Engine: 2.7.2.376379, Antispam-Data: 2012.4.3.91229 Subject: Re: [Caml-list] exn vs option 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 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 > écrit : > > > 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 > >