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 q356jdWN006159 for ; Thu, 5 Apr 2012 08:45:40 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjoBAEQ/fU/RVda2kGdsb2JhbABDhXyyYggiAQEBAQkJDQcUBCOCCQEBAQQSAg8EGQEbHQEDDAYDAgsNAgIRFQICIgERAQUBHAYTIoddAQMLC6EiCotIToJxhHIKGScNV4EOAQULgSSLcIIYgRgElWiBEY1CPYQM X-IronPort-AV: E=Sophos;i="4.75,374,1330902000"; d="scan'208";a="139133175" Received: from mail-ob0-f182.google.com ([209.85.214.182]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 05 Apr 2012 08:45:29 +0200 Received: by obbwc18 with SMTP id wc18so2310648obb.27 for ; Wed, 04 Apr 2012 23:45:28 -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:content-transfer-encoding; bh=ukgnFB16nCo2M2ZF5APrGE5hNU1vNQeBe58wFFb3V5o=; b=mRXpKez/hydRNxR/ty5FM6HJQ27eK6mh2lUDgbzgtGbiUrlukrnhBRuqggM9m95LG3 Is672PyvsadGbaLhWQLWMDu2hUJxJwpSHcx6nStM0JaQ17LBHH+APCKBSIcGCrcW1oty t11OQlXqU10o3SIMJEbfgvjR9LBUeR9g/qgHZVAuPFhXOQTme3PB6jalcCdRuB6id2gT UZ7uDseKmecRGIz9usCX+dgRVRyoq9bl5yOrU17TRxNaX2JOs75wzQTAJDmzX15+mnpN oX9wUEkxkw1u+yjo4CV9VdVa+fvHbnd2G/1iRoQ/q1XzRP8BZIl28jLAAgwYTZjd6cx2 pONQ== MIME-Version: 1.0 Received: by 10.182.89.101 with SMTP id bn5mr1838520obb.39.1333608328596; Wed, 04 Apr 2012 23:45:28 -0700 (PDT) Received: by 10.76.137.45 with HTTP; Wed, 4 Apr 2012 23:45:28 -0700 (PDT) In-Reply-To: References: Date: Thu, 5 Apr 2012 08:45:28 +0200 Message-ID: From: Raphael Proust To: Pierre Chopin Cc: caml-list@inria.fr Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id q356jdWN006159 Subject: Re: [Caml-list] exn vs option 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 rather than OCaml, but it still applies). On Wed, Apr 4, 2012 at 10:25 PM, Pierre Chopin wrote: > 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 sense, performance wise, to use exception > instead of 'a option ? > > test1.ml > ---------------------------------------------------------------------- > > exception Foo > let f x = >  if x =1 then raise Foo else () > > ;; > >  for i = 0 to 10_000_000 do > try >     f 1 > with Foo -> () > done > ------------------------------------------------------------------------ > test2.ml: > ------------------------------------------------------------------------ > let f x = >     if x=1 then None else Some () > > ;; > for i = 0 to 10_000_000 do >     match f 1 with >         None -> () >     |   Some s -> s >     done > ------------------------------------------------------------------------ > > > > -- > Pierre Chopin, > Chief Technology Officer and co-founder > punchup LLC > pierre@punchup.com > -- _______ Raphael