2016-11-26 73 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