[erlang-questions] eep: multiple patterns

Per Melin <>
Sat May 31 11:05:28 CEST 2008


2008/5/31 Christopher Atkins <>:
> If I uncomment the line in [loop1/0] below, performance for
> that loop degrades by an order of magnitude.  Why is that?

You have a bug: N rem 2 =:= 2 can never be true. So you're only
sending 'test1' messages. If you fix that, you should see very
different results, even though you're only measuring the time it takes
to *send* the messages. A tip is to lower the number of messages to
100000 unless you want to bring your machine to its knees.

(I apologize to everyone for hijacking this thread.)



> -module(test_receive).
> -compile(export_all).
>
> start() ->
>         statistics(runtime),
>         statistics(wall_clock),
>         PidLoop1 = spawn(?MODULE, loop1,[]),
>         sender(PidLoop1, 10000000),
>         {_, Loop1Time1} = statistics(runtime),
>         {_, Loop1Time2} = statistics(wall_clock),
>         io:format("Sent ~p messages in ~p /~p~n", [100000, Loop1Time1,
> Loop1Time2]),
>         statistics(runtime),
>         statistics(wall_clock),
>         PidLoop2 = spawn(?MODULE, loop2,[]),
>         sender(PidLoop2, 10000000),
>         {_, Loop2Time1} = statistics(runtime),
>         {_, Loop2Time2} = statistics(wall_clock),
>         io:format("Sent ~p messages in ~p /~p~n", [100000, Loop2Time1,
> Loop2Time2]).
>
> sender(_, 0) -> void;
> sender(Pid, N) ->
>         if
>           N rem 2 =:= 2 ->
>                 Pid ! test2;
>           true ->
>                 Pid ! test1
>         end,
>         sender(Pid, N - 1).
>
> proc1(F) ->
>         receive
>                 start -> spawn_link(F)
>         end.
>
> loop1() ->
>         receive
>                 %%test1 -> loop1();
>                 test2 -> loop1()
>         end.
>
> loop2() ->
>         receive
>                 _ -> loop2()
>         end.
>



More information about the erlang-questions mailing list