简体   繁体   English

呼叫链接的性能提升?

[英]Performance gain on call chaining?

Do you gain any performance, even if it's minor, by chaining function calls as shown below or is it just coding style preference? 您是通过链接如下所示的函数调用获得任何性能,即使它是次要的,还是只是编码样式首选项?

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

Instead of 代替

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

I was thinking whether in the 2nd version the garbage collector has some work to do for S1 , S2 , S3 . 我在想是否在第二版中垃圾收集器是否有一些工作要做S1S2S3 Should that apply for the 1st version as well? 这应该适用于第一版吗?

They are identical after compilation. 编译后它们是相同的。 You can confirm this by running the erl file through erlc -S and reading the generated .S file: 您可以通过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}.

...

As you can see, both execute1 and execute2 result in identical code (the only thing different are line numbers and label numbers. 正如您所看到的, execute1execute2产生相同的代码(唯一不同的是行号和标签号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM