[erlang-questions] eep: multiple patterns

Per Melin per.melin@REDACTED
Fri May 30 18:07:18 CEST 2008


2008/5/30 Per Melin <per.melin@REDACTED>:
> If I send 100k 'foo' messages and then 100k 'bar' messages to a
> process, and then do a catch-all receive until there are no messages
> left, that takes 0.03 seconds.
>
> If I do a selective receive of only the 'bar' messages, it takes 90 seconds.

I found my old test code:

-module(selective).

-export([a/2, c/2]).

a(Atom, N) ->
    spawn(fun() -> b(Atom, N) end).

b(Atom, N) ->
    spam_me(foo, N),
    spam_me(bar, N),
    R = timer:tc(?MODULE, c, [Atom, N]),
    io:format("TC: ~p~n", [R]).

c(Atom, N) ->
    receive
        Atom -> c(Atom, N - 1)
    after 0 ->
        N
    end.

spam_me(Msg, Copies) ->
    lists:foreach(fun(_) -> self() ! Msg end, lists:duplicate(Copies, 0)).

---

2> selective:a(bar, 100000).
<0.38.0>
TC: {124130689,0}
3> selective:a(foo, 100000).
<0.40.0>
TC: {23176,0}



More information about the erlang-questions mailing list