开发者

Erlang argument matching and performance

开发者 https://www.devze.com 2023-02-11 20:59 出处:网络
Any difference between these two from a performance (or any other) perspective? f1(X=whatever) -> ok; (more...开发者_开发知识库)

Any difference between these two from a performance (or any other) perspective?

f1(X=whatever) -> ok; (more...开发者_开发知识库)

and

f2(whatever=X) -> ok; (more...)


You can examine the code the compiler produces by compiling with -S which generates a .S file. You'll get something like this and you can confirm that the compiler generates exactly the same code for each of your two cases.

{function, f1, 1, 12}.
{label,11}.
{func_info,{atom,test},{atom,f1},1}.
{label,12}.
{test,is_eq_exact,{f,11},[{x,0},{atom,whatever}]}.
return.

Personally, I find "whatever=X" counter-intuitive and harder to read.


The = in a pattern means that the LHS and RHS are aliases referring to the data same value. Both sides have to match the value so writing something {X}=[Y] will never match (and the compiler will complain). It is most often used like {X,Y}=T which allows you to both match and pull apart the data and still have a reference to the whole structure. Both to have your cake and eat it. Note that it can be used anywhere in a pattern and not just at the top level so you can use it like {foo,[H|T]=A,B,C}.

There is no performance difference.

0

精彩评论

暂无评论...
验证码 换一张
取 消