zoukankan      html  css  js  c++  java
  • [Erlang0013][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。
     
  • 相关阅读:
    尽解powershell的workflow
    powershell玩转iis网站服务器
    Windows Terminal (Preview)治好了cmd,powershell的癌症
    博客园“NET Core 版博客系统”的运维浅见
    此贴告诉你:为啥shell脚本人,不建议学python
    关于revit的外部扩展存储
    Revit二次开发 屏蔽复制构件产生的重复类型提示窗
    xpath测试工具 xpath调试工具
    c# 防止重复运行 弹出已运行窗口并传递消息
    .NET APlayer播放器 demo
  • 原文地址:https://www.cnblogs.com/liangjingyang/p/2850748.html
Copyright © 2011-2022 走看看