[Erlang-0013][OTHERS] reductions计数
缘起坚强哥分享霸爷对于公平调度的解答。
===========================================================================
如果函数A会调用函数B, 函数B每次调用都会递归3次。那么每调用一次函数A,是否就是4个reduction?
到底是怎样自己动手测一下吧:
先是不嵌套的
Eshell V5.9 (abort with ^G)
1> A = spawn(fun() -> one:start() end).
<0.33.0>
2> process_info(A, reductions).
{reductions,46}
3> process_info(A, reductions).
{reductions,46}
4> process_info(A, reductions).
{reductions,46}
5> process_info(A, reductions).
{reductions,46}
6> A!go.
go
7> process_info(A, reductions).
{reductions,49}
代码:
-module(one).
-compile(export_all).
start() ->
receive
go ->
aaa(3)
end,
receive
stop ->
ok
end,
ok.
aaa(1) ->
ok;
aaa(N) ->
aaa(N-1).
========================================================
改为嵌套调用:
1> A = spawn(fun() -> one:start() end).
<0.33.0>
2> process_info(A, reductions).
{reductions,46}
3> A!go.
go
4> process_info(A, reductions).
{reductions,50}
代码:
-module(one).
-compile(export_all).
start() ->
receive
go ->
bbb()
end,
receive
stop ->
ok
end,
ok.
bbb() ->
aaa(3).
aaa(1) ->
ok;
aaa(N) ->
aaa(N-1).
===================================================
receive会不会有影响呢?
不会的。。。
代码:
-module(one).
-compile(export_all).
start() ->
receive
go ->
bbb()
end,
receive
rec ->
ok
end,
receive
stop ->
stop
end,
ok.
bbb() ->
aaa(3).
aaa(1) ->
ok;
aaa(N) ->
aaa(N-1).
综上,reduction是函数调用,如果函数A会调用函数B, 函数B每次调用都会递归3次。那么每调用一次函数A,就是4个reduction。