2016-11-26 56 views
5

您是否通过链接如下所示的函数调用来获得任何性能,即使它很小,还是仅仅是编码风格偏好?通话链接的性能提升?

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

而不是

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

我在想是否在第二版的垃圾收集器有一定的工作S1S2S3做。是否也适用于第一版?

回答

5

编译后它们是相同的。您可以通过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结果相同的代码(唯一不同,是行号和标签号。

+0

我无法找到与文档中汇编代码生成相关的'-S'选项的任何引用。它声明'.S'文件包含汇编代码,但它不会告诉您如何生成它们。 http://erlang.org/doc/man/erlc.html – ipinak