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。
     
  • 相关阅读:
    搭建Maven版SSM工程
    mac终端常用的命令
    常见的HTTP请求错误
    Go通关03:控制结构,if、for、switch逻辑语句
    Go通关14:参数传递中,值、引用及指针之间的区别
    Go通关13:究竟在什么情况下才使用指针?
    Go通关12:如何写出高效的并发模式?
    Go通关11:并发控制神器之Context深入浅出
    Go通关10:并发控制,同步原语 sync 包
    Go通关09:并发掌握,goroutine和channel声明与使用!
  • 原文地址:https://www.cnblogs.com/liangjingyang/p/2850748.html
Copyright © 2011-2022 走看看