OCaml pattern matching performance

Say I have this type type 'a tree = Node of int * 'a * 'a tree

The part intis this rank.

I also have a function let rank = function Node (r, _, _) -> r.

Suppose in my real code I have many places that need to access ranknode and most of the time rankfor the same nodes.

My question is: should I always use the function rank tdirectly or do I need to assign the rank t, say r, and then use it r?

For example, I can do

if rank t1 < rank t2 then Node (rank t1 + 1, 5, t1)
else Node (rank t2 + 1, 5, t2)

or

let r1 = rank t1 in let r2 = rank t2 in
if r1 < r2 then Node (r1+1, 5, t1) else Node (r2, 5, t2)

What is a performance difference? And which way is better and why?

+4
source share
1 answer

, . , OCaml ; - .

, ;)

+4

All Articles