当我有这样的代码时:
receive do
{:hello, msg} -> msg
end
让我说我的邮箱里有N
条消息。查找此特定邮件O(1)
,O(N)
或其他内容的效果是什么?
答案 0 :(得分: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.)
所以在你的情况下,它是O(N)操作。
答案 1 :(得分:1)
性能将线性增长,并与邮箱中的元素数量成正比,因此为O(N)。
答案 2 :(得分:1)
Erlang的消息,因此Elixir,是“先进先出”。您逐个浏览它们,并处理满足receive
中任何条款的第一个。在最糟糕的情况下,您可以阻止消息框。