呼叫链接的性能提升?

时间:2016-11-26 20:35:30

标签: performance erlang

您是通过链接如下所示的函数调用获得任何性能,即使它是次要的,还是只是编码样式首选项?

execute() -> 
   step4(step3(step2(step1())).

而不是

execute() ->
   S1 = step1(),
   S2 = step2(S1),
   S3 = step3(S2),
   step4(S3).

我在考虑垃圾收集器是否在第二版中为S1S2S3做了一些工作。这是否也适用于第一版?

1 个答案:

答案 0 :(得分:4)

编译后它们是相同的。您可以通过erlc -S运行erl文件并阅读生成的.S文件来确认这一点:

$ cat a.erl
-module(a).
-compile(export_all).

step1() -> ok.
step2(_) -> ok.
step3(_) -> ok.
step4(_) -> ok.

execute1() ->
   step4(step3(step2(step1()))).

execute2() ->
   S1 = step1(),
   S2 = step2(S1),
   S3 = step3(S2),
   step4(S3).
$ erlc -S a.erl
$ cat a.S
{module, a}.  %% version = 0

...

{function, execute1, 0, 10}.
  {label,9}.
    {line,[{location,"a.erl",9}]}.
    {func_info,{atom,a},{atom,execute1},0}.
  {label,10}.
    {allocate,0,0}.
    {line,[{location,"a.erl",10}]}.
    {call,0,{f,2}}.
    {line,[{location,"a.erl",10}]}.
    {call,1,{f,4}}.
    {line,[{location,"a.erl",10}]}.
    {call,1,{f,6}}.
    {call_last,1,{f,8},0}.


{function, execute2, 0, 12}.
  {label,11}.
    {line,[{location,"a.erl",12}]}.
    {func_info,{atom,a},{atom,execute2},0}.
  {label,12}.
    {allocate,0,0}.
    {line,[{location,"a.erl",13}]}.
    {call,0,{f,2}}.
    {line,[{location,"a.erl",14}]}.
    {call,1,{f,4}}.
    {line,[{location,"a.erl",15}]}.
    {call,1,{f,6}}.
    {call_last,1,{f,8},0}.

...

正如您所看到的,execute1execute2都会产生相同的代码(唯一不同的是行号和标签号。