OCaml performance according to matching order

In OCaml, is there any relation between the order in a pattern-matching and performance?

For instance, if I declare a type:

type t = A | B | C

and then perform some pattern-matching as follows:

match t1 with
  | A -> ...
  | _ -> ...

From a performance point of view, is it equivalent to

match t1 with
  | B -> ...
  | _ -> ...

assuming in the first case there are as many A's as there are B's in the second?

In other words, should I worry about the order of declaration of constructors in a type, when considering performance?

Answers


There is a paper explaining how pattern-matching is compiled in OCaml: “Optimizing Pattern Matching”, L. Maranget and F. Le Fessant, ICFP’01

It basically says that the semantics is "in order", but that it is usually compiled in the optimal way, independently of the order of lines. Values of constructors don't matter either, it's the number of constructors that makes the difference, i.e. if it is compiled by a tree of comparisons, or by a jump table.

Optimality + exhaustivity test makes pattern-matching in OCaml probably the most wonderful feature of the language, and is much more efficient that writting cascades of "if" manually.


Need Your Help

VS 2012 Support for Windows mobile 6

c# visual-studio windows-mobile windows-ce windows-mobile-6.5

I've read here that Microsoft will add support for Windows CE in visual studio 2012, but I haven't seen this project template in VS 2012.

Android write access to SD card

java android android-externalstorage

I am trying to write to External secondary storage (sd card), but it looks like I do not have the permission.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.