From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by sympa.inria.fr (Postfix) with ESMTPS id E9BEC7EE56 for ; Fri, 7 Dec 2012 03:45:21 +0100 (CET) Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of garrigue@math.nagoya-u.ac.jp) identity=pra; client-ip=133.6.130.5; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="garrigue@math.nagoya-u.ac.jp"; x-sender="garrigue@math.nagoya-u.ac.jp"; x-conformance=sidf_compatible Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of garrigue@math.nagoya-u.ac.jp) identity=mailfrom; client-ip=133.6.130.5; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="garrigue@math.nagoya-u.ac.jp"; x-sender="garrigue@math.nagoya-u.ac.jp"; x-conformance=sidf_compatible Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mailhost.math.nagoya-u.ac.jp) identity=helo; client-ip=133.6.130.5; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="garrigue@math.nagoya-u.ac.jp"; x-sender="postmaster@mailhost.math.nagoya-u.ac.jp"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap4EAK9WwVCFBoIF/2dsb2JhbABEvktzgh4BAQQBJxwBNQIDCws0ElcGE4gKBa8chD8ChgaJAQeQG2GIYI0mkEeDAg X-IronPort-AV: E=Sophos;i="4.84,234,1355094000"; d="scan'208";a="165048904" Received: from rabbit.math.nagoya-u.ac.jp (HELO mailhost.math.nagoya-u.ac.jp) ([133.6.130.5]) by mail4-smtp-sop.national.inria.fr with ESMTP; 07 Dec 2012 03:45:19 +0100 Received: from mailhost.math.nagoya-u.ac.jp (localhost [127.0.0.1]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTP id 5FED26389; Fri, 7 Dec 2012 11:45:16 +0900 (JST) Received: from mailhost.math.nagoya-u.ac.jp (localhost [127.0.0.1]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTP id F29DB251A; Fri, 7 Dec 2012 11:45:13 +0900 (JST) DomainKey-Signature: h=Received:Subject:Mime-Version:Content-Type:From:In-Reply-To:Date:Cc:Content-Transfer-Encoding:Message-Id:References:To:X-Mailer; b=; c=nofws; d=math.nagoya-u.ac.jp; q=; s=alpha Received: from tanpopo.garrigue.jp (58x158x128x157.ap58.ftth.ucom.ne.jp [58.158.128.157]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTPSA id 37B5F252A; Fri, 7 Dec 2012 11:45:09 +0900 (JST) Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Content-Type: text/plain; charset=iso-8859-1 From: Jacques Garrigue In-Reply-To: <50C15590.1010507@riken.jp> Date: Fri, 7 Dec 2012 11:45:07 +0900 Cc: caml-list@inria.fr Content-Transfer-Encoding: 7bit Message-Id: References: <50C15590.1010507@riken.jp> To: Francois Berenger X-Mailer: Apple Mail (2.1499) Subject: Re: [Caml-list] What is the fastest? Pattern matching or if then else On 2012/12/07, at 11:33, Francois Berenger wrote: > Hello, > > I always wondered what is the fastest (at least for integers): > > match n with > 0 -> (* do something *) > | _ -> (* do something else *) > > or > > if n = 0 then > (* do something *) > else > (* do something else *) > > Sometimes I have some code that would be more beautiful > if I was using only pattern matching rather than > pattern matching interspersed with if then else directives. > > Is one significantly faster than the other? > > Is it the same cost at runtime? $ ocaml -dlambda OCaml version 4.00.1 # fun n -> match n with 0 -> "this" | _ -> "that";; (function n/1016 (if (!= n/1016 0) "that" "this")) - : int -> string = # fun n -> if n then "this" else "that";; (function n/1017 (if n/1017 "this" "that")) - : bool -> string = In most cases, the two are going to be exactly equivalent. Here there is a slight difference because of the handling of the constant 0. But I would expect that to be optimized out in the native code compiler. So the short answer is: do not let your sense of beauty be disturbed by that. Jacques