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 q34KcZec013888 for ; Wed, 4 Apr 2012 22:38:35 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvQCADGwfE8SB0Qim2dsb2JhbABFuCoiAQEBAQEICRQUJ4IJAQEEAXkFCwtGVwaIFwixDIkJkE8EqRY X-IronPort-AV: E=Sophos;i="4.75,370,1330902000"; d="scan'208";a="152746126" Received: from dmz-mailsec-scanner-5.mit.edu ([18.7.68.34]) by mail1-smtp-roc.national.inria.fr with ESMTP; 04 Apr 2012 22:38:29 +0200 X-AuditID: 12074422-b7fd66d0000008f9-02-4f7cb143c24d Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) by dmz-mailsec-scanner-5.mit.edu (Symantec Messaging Gateway) with SMTP id 13.A9.02297.341BC7F4; Wed, 4 Apr 2012 16:38:27 -0400 (EDT) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q34KcQnX010992; Wed, 4 Apr 2012 16:38:26 -0400 Received: from localhost (CONTENTS-VNDER-PRESSVRE.MIT.EDU [18.9.64.11]) (authenticated bits=0) (User authenticated as jfc@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q34KcPcj001730; Wed, 4 Apr 2012 16:38:26 -0400 (EDT) Message-Id: <201204042038.q34KcPcj001730@outgoing.mit.edu> To: Pierre Chopin cc: caml-list@inria.fr In-reply-to: References: Comments: In-reply-to Pierre Chopin message dated "Wed, 04 Apr 2012 16:25:48 -0400." X-Mailer: MH-E 8.2; nmh 1.3; GNU Emacs 23.1.1 Date: Wed, 04 Apr 2012 16:38:25 -0400 From: John Carr X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrFIsWRmVeSWpSXmKPExsUixG6nruu8scbf4NVFHYtPOzawWDROOMDu wOQx6cUhFo/rvzsYA5iiuGxSUnMyy1KL9O0SuDJ2/ZrLXPCAs+L12voGxqvsXYycHBICJhLT XyxkhbDFJC7cW8/WxcjFISSwj1Hi5qlbTBDOekaJgwcnMYFUCQn8Z5R49b0axOYVsJKY8rIP LC4ioCbReeQ3C4jNDDTp1cMHYBuEgeLzN61iBrE5BQIlug6/YoSYEyDxcd5fRoj6XInDXbNY IK7Qlfi4aCZQLwcHi4CqxPoGO5Awm4CsxKP2LsYJjPwLGBlWMcqm5Fbp5iZm5hSnJusWJyfm 5aUW6Zrq5WaW6KWmlG5iBAeRi9IOxp8HlQ4xCnAwKvHw6k6u8RdiTSwrrsw9xCjJwaQkyjtv FVCILyk/pTIjsTgjvqg0J7X4EKMEB7OSCK/kAqAcb0piZVVqUT5MSpqDRUmcV13rnZ+QQHpi SWp2ampBahFMVoaDQ0mCd8oGoEbBotT01Iq0zJwShDQTByfIcB6g4VfWgwwvLkjMLc5Mh8if YlSUEudtA2kWAElklObB9cKi/BWjONArwrxVIFU8wAQB1/0KaDAT0OCTX6tBBpckIqSkGhjZ lvU9WFSuqj9dTK9qymkbt8AF8wqW/pnG3L3zE7udtG+S5Gyvee09r5fvXcYRUWcxJUfoaLHP 3TU5G09oeEjOLJgVsMvu+9qEDwvkfpdYaHwq8en1CyvcEXsyO0U6kvXyohdRwS5zX36TkxUP dozRka9h9+Zm4+tuPVqX8GSZx7YnG02fVyixFGckGmoxFxUnAgCoA4ZNzQIAAA== Subject: Re: [Caml-list] exn vs option 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 ?