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 q34KPtY2013523 for ; Wed, 4 Apr 2012 22:25:55 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtYBAFWtfE/RVdc2imdsb2JhbABDpxwBiCMBiG4IIgEBAQoJDQcSBiOCIgITcQddEgEFARYfIodnnh+CXQqVO40rgyQElWOBEYokgxs9hCY X-IronPort-AV: E=Sophos;i="4.75,370,1330902000"; d="scan'208";a="139099240" Received: from mail-lpp01m010-f54.google.com ([209.85.215.54]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 04 Apr 2012 22:25:49 +0200 Received: by lagv3 with SMTP id v3so1384164lag.27 for ; Wed, 04 Apr 2012 13:25:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type :x-gm-message-state; bh=uG+BBJe1AxSRj+17EGOFJCoHeocPRP/3DCeUDVudmPw=; b=n6H5b3TdDDWFkFpi5G/CPnqod3GCbITmyvUSDtE1y+QldUTKpvIpzx2CDhO+3aWXPT 393wxWhKOrlAiijZGeFv9tHNNATDXx1Bm2Hp5HdarL6a232+1QYatJjx93AIXSFqO+/T JUaIMm4OD8w2ncA/wkRevfn1D7aXCkWIeGZEw4xwjWAY3Ktho1wFDQl8+sCh2xGFk1dG hXc2vefwhsEr/nCwNDPgELUzxxuuMpcVwzkc7x+iTCeIOEpGpLKYgrlV079Fdfr0E2gN kZz0xBeUcb2+HhXMnlFxSGjU4c0rHZ4bFMIg8ovcKJfrU2B+mhBy4VGep/dWSQkDOZe/ Hn6w== MIME-Version: 1.0 Received: by 10.152.123.229 with SMTP id md5mr20210180lab.34.1333571149046; Wed, 04 Apr 2012 13:25:49 -0700 (PDT) Received: by 10.112.55.2 with HTTP; Wed, 4 Apr 2012 13:25:48 -0700 (PDT) Date: Wed, 4 Apr 2012 16:25:48 -0400 Message-ID: From: Pierre Chopin To: caml-list@inria.fr Content-Type: multipart/alternative; boundary=f46d04426aa49f224b04bce03a03 X-Gm-Message-State: ALoCoQkRPZeNLtkmNOiMF9Jak6ccpcVfM3P5Tl3Cu1ubO9RqiR7+XQraEVuT4mUfri2cC/oB4rNG Subject: [Caml-list] exn vs option --f46d04426aa49f224b04bce03a03 Content-Type: text/plain; charset=ISO-8859-1 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 --f46d04426aa49f224b04bce03a03 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hi,

I benchmarked two programs, in one case t= he main function throw an exception that is caught, in the other the functi= on 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 =3D
=A0if x= =3D1 then raise Foo else ()

;;

=A0for i =3D 0 to 10_000_00= 0 do
try
=A0 =A0 f 1
with Foo -> ()
<= div>done
--------------------------------------------------= ----------------------
--------= ----------------------------------------------------------------
let f x =3D
=A0 =A0 if x=3D1 then None else Some ()

;;
for i =3D 0 to 10_000_000 do
=A0 =A0 matc= h f 1 with
=A0 =A0 =A0 =A0 None -> ()
=A0 =A0 | =A0 = Some s -> s
=A0 =A0 done
----------------= --------------------------------------------------------



--
Pierre= Chopin,
Chief Technology Officer and co-founder
punchup LLC<= /div>
--f46d04426aa49f224b04bce03a03--