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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id EBD7C7EE57 for ; Fri, 7 Dec 2012 10:28:49 +0100 (CET) Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of loverdos@gmail.com) identity=pra; client-ip=74.125.83.54; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="loverdos@gmail.com"; x-sender="loverdos@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail1-smtp-roc.national.inria.fr: domain of loverdos@gmail.com designates 74.125.83.54 as permitted sender) identity=mailfrom; client-ip=74.125.83.54; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="loverdos@gmail.com"; x-sender="loverdos@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-ee0-f54.google.com) identity=helo; client-ip=74.125.83.54; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="loverdos@gmail.com"; x-sender="postmaster@mail-ee0-f54.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqwBAGG1wVBKfVM2kGdsb2JhbABEDoJeW1unVJI5CBYOAQEBAQkJDQcUBCOCHgEBBAEnGQEbEgsBAwELBgULDQ0hIxEBBQEKEgYTEodrAQMJBgELpEaMM4J6hGsKGScDClmIdQEFDIwzg2JhA5YDgRyNSz+DVkA X-IronPort-AV: E=Sophos;i="4.84,236,1355094000"; d="scan'208";a="185065804" Received: from mail-ee0-f54.google.com ([74.125.83.54]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 07 Dec 2012 10:28:49 +0100 Received: by mail-ee0-f54.google.com with SMTP id c13so231035eek.27 for ; Fri, 07 Dec 2012 01:28:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=RSRikZks2tsdAjflEnZ73ZYqlZDj+ihDwzgRtovtNKE=; b=DWW+9X9qKcaA77dvMb9W5k/SyuI0jwQdIQhxkqx+koir4Krk6Qhseos4rKEkloSIAA 9qHv6ThikwDOV9CRuMM6OW5oB8l11IzUXcBtcSnClTWsXSGqDHLSOUGSetPSY/8eVpmh FHMrGe35SRtAXoTXs1DBSij+AXdc5NO7Bgt7SH+WENJgdRucIlSiL+rgZ966k9U/I9o9 vPhAWcVoJoMLBRa+znIvE68boukfy5Cr6Yjd6DAoP2Z+NaD0w6ZoJMIZ5ufYb6/ESzCW 5a6M3nrDDHE95baQDmFBKX5jE5OaUsO5hkmU0st3uVDGQ8jpeqvjAaeWaPGEoJFjrklx g11Q== Received: by 10.14.213.134 with SMTP id a6mr14634414eep.45.1354872529184; Fri, 07 Dec 2012 01:28:49 -0800 (PST) Received: from ?IPv6:2001:648:2320:1:18cc:c829:e7b1:9ab2? ([2001:648:2320:1:18cc:c829:e7b1:9ab2]) by mx.google.com with ESMTPS id z8sm21488494eeo.11.2012.12.07.01.28.46 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 07 Dec 2012 01:28:47 -0800 (PST) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) From: Christos KK Loverdos In-Reply-To: Date: Fri, 7 Dec 2012 11:28:48 +0200 Cc: Francois Berenger , caml-list@inria.fr Content-Transfer-Encoding: quoted-printable Message-Id: <5585B228-0372-477D-B118-4CD3E5210684@gmail.com> References: <50C15590.1010507@riken.jp> To: Jacques Garrigue X-Mailer: Apple Mail (2.1499) Subject: Re: [Caml-list] What is the fastest? Pattern matching or if then else On Dec 7, 2012, at 4:45 AM, Jacques Garrigue = wrote: > On 2012/12/07, at 11:33, Francois Berenger wrote: >=20 >> Hello, >>=20 >> I always wondered what is the fastest (at least for integers): >>=20 >> match n with >> 0 -> (* do something *) >> | _ -> (* do something else *) >>=20 >> or >>=20 >> if n =3D 0 then >> (* do something *) >> else >> (* do something else *) >>=20 >> 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. >>=20 >> Is one significantly faster than the other? >>=20 >> Is it the same cost at runtime? >=20 > $ ocaml -dlambda > OCaml version 4.00.1 >=20 > # fun n -> match n with 0 -> "this" | _ -> "that";; > (function n/1016 (if (!=3D n/1016 0) "that" "this")) > - : int -> string =3D > # fun n -> if n then "this" else "that";; > (function n/1017 (if n/1017 "this" "that")) > - : bool -> string =3D >=20 > 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. +1 >=20 > Jacques >=20 >=20 > --=20 > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs -- Christos KK Loverdos @loverdos stepsinscala.com Everything that is really great and inspiring is created by the individual = who can labor in freedom =97 Albert Einstein