caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] What is the fastest? Pattern matching or if then else
@ 2012-12-07  2:33 Francois Berenger
  2012-12-07  2:45 ` Jacques Garrigue
       [not found] ` <20121207.090650.972729327013546587.Christophe.Troestler@umons.ac.be>
  0 siblings, 2 replies; 5+ messages in thread
From: Francois Berenger @ 2012-12-07  2:33 UTC (permalink / raw)
  To: caml-list

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?

Thanks a lot,
F.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] What is the fastest? Pattern matching or if then else
  2012-12-07  2:33 [Caml-list] What is the fastest? Pattern matching or if then else Francois Berenger
@ 2012-12-07  2:45 ` Jacques Garrigue
  2012-12-07  9:28   ` Christos KK Loverdos
       [not found] ` <20121207.090650.972729327013546587.Christophe.Troestler@umons.ac.be>
  1 sibling, 1 reply; 5+ messages in thread
From: Jacques Garrigue @ 2012-12-07  2:45 UTC (permalink / raw)
  To: Francois Berenger; +Cc: caml-list

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.

	Jacques


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] What is the fastest? Pattern matching or if then else
  2012-12-07  2:45 ` Jacques Garrigue
@ 2012-12-07  9:28   ` Christos KK Loverdos
  0 siblings, 0 replies; 5+ messages in thread
From: Christos KK Loverdos @ 2012-12-07  9:28 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: Francois Berenger, caml-list


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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] What is the fastest? Pattern matching or if then else
       [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>
  0 siblings, 1 reply; 5+ messages in thread
From: Francois Berenger @ 2012-12-10  0:06 UTC (permalink / raw)
  To: caml-list

On 12/07/2012 05:06 PM, Christophe TROESTLER wrote:
> On Fri, 7 Dec 2012 11:33:52 +0900, Francois Berenger wrote:
>>
>> Hello,
>>
>> I always wondered what is the fastest (at least for integers):
>
> In general, you can check these things yourself:
> https://github.com/Chris00/ocaml-benchmark

Christophe TROESTLER even did an OPAM package for benchmark:

opam update
opam install benchmark

Thanks!
F.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] What is the fastest? Pattern matching or if then else
       [not found]     ` <20121210.012739.1808775350475077852.Christophe.Troestler@umons.ac.be>
@ 2012-12-10  0:44       ` Francois Berenger
  0 siblings, 0 replies; 5+ messages in thread
From: Francois Berenger @ 2012-12-10  0:44 UTC (permalink / raw)
  To: caml-list

On 12/10/2012 09:27 AM, Christophe TROESTLER wrote:
> On Mon, 10 Dec 2012 09:06:14 +0900, Francois Berenger wrote:
>>
>> On 12/07/2012 05:06 PM, Christophe TROESTLER wrote:
>>> On Fri, 7 Dec 2012 11:33:52 +0900, Francois Berenger wrote:
>>>>
>>>> Hello,
>>>>
>>>> I always wondered what is the fastest (at least for integers):
>>>
>>> In general, you can check these things yourself:
>>> https://github.com/Chris00/ocaml-benchmark
>>
>> Christophe TROESTLER even did an OPAM package for benchmark:
>>
>> opam update
>> opam install benchmark
>>
>> Thanks!
>
> Well, I have not done it, it seems we should rather thank Anil!

Sorry!


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-12-10  0:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-07  2:33 [Caml-list] What is the fastest? Pattern matching or if then else Francois Berenger
2012-12-07  2:45 ` Jacques Garrigue
2012-12-07  9:28   ` Christos KK Loverdos
     [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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).