From: Christos KK Loverdos <loverdos@gmail.com>
To: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
Cc: Francois Berenger <berenger@riken.jp>, caml-list@inria.fr
Subject: Re: [Caml-list] What is the fastest? Pattern matching or if then else
Date: Fri, 7 Dec 2012 11:28:48 +0200 [thread overview]
Message-ID: <5585B228-0372-477D-B118-4CD3E5210684@gmail.com> (raw)
In-Reply-To: <D5166908-FEE6-46E6-8D3D-2C2261038374@math.nagoya-u.ac.jp>
On Dec 7, 2012, at 4:45 AM, Jacques Garrigue <garrigue@math.nagoya-u.ac.jp> wrote:
> On 2012/12/07, at 11:33, Francois Berenger <berenger@riken.jp> 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>
> # fun n -> if n then "this" else "that";;
> (function n/1017 (if n/1017 "this" "that"))
> - : bool -> string = <fun>
>
> 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
>
> Jacques
>
>
> --
> 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 — Albert Einstein
next prev parent reply other threads:[~2012-12-07 9:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-12-07 2:33 Francois Berenger
2012-12-07 2:45 ` Jacques Garrigue
2012-12-07 9:28 ` Christos KK Loverdos [this message]
[not found] ` <20121207.090650.972729327013546587.Christophe.Troestler@umons.ac.be>
2012-12-10 0:06 ` Francois Berenger
[not found] ` <20121210.012739.1808775350475077852.Christophe.Troestler@umons.ac.be>
2012-12-10 0:44 ` Francois Berenger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5585B228-0372-477D-B118-4CD3E5210684@gmail.com \
--to=loverdos@gmail.com \
--cc=berenger@riken.jp \
--cc=caml-list@inria.fr \
--cc=garrigue@math.nagoya-u.ac.jp \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox