Elixir/Erlang中选择性`receive`的表现

Der*_*ang 2 erlang elixir

当我有这样的代码:

receive do
  {:hello, msg} -> msg
end
Run Code Online (Sandbox Code Playgroud)

让我们说N我的邮箱里有邮件.觉得这次特定消息的性能O(1),O(N)或者介于两者之间?

Hyn*_*dil 6

接收执行消息框的线性扫描,然后返回匹配的第一个.有一个例外(自R14A起)

OTP-8623  == compiler erts hipe stdlib ==

    Receive statements that can only read out a newly created
    reference are now specially optimized so that it will execute
    in constant time regardless of the number of messages in the
    receive queue for the process. That optimization will benefit
    calls to gen_server:call(). (See gen:do_call/4 for an example
    of a receive statement that will be optimized.)
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下它是O(N)操作.