[erlang-questions] OTP process startup vs spawn overhead

Hunter Kelly retnuh@REDACTED
Thu Jul 28 12:12:16 CEST 2011


If I had to guess, it's because you're creating a closure each time
through the loop.

Try something like

recv() ->
  receive _ -> ok end.

And then in loop:

spawn_link(?MODULE, recv, [])


By the way, list comprehensions can have multiple generator
statements, so you don't have to nest them like that.  I think you
could do:

Fun = fun() -> [ Pid ! ok || _ <- lists:seq(1,10000), Pid <-
spawn_link(?MODULE, recv, []) ] end,
[ timer:tc(Fun) || _ lists:seq(1,10) ].

to make it a bit more reasonable.  This will also remove the cost of
creating the closure in the timer statement, too.

H

On Thu, Jul 28, 2011 at 11:00 AM, Heinrich Venter <hfventer@REDACTED> wrote:
> In an attempt to prove that spinning up OTP processes as workers does
> not incur that much overhead I have done the following with results
> that are confusing me a bit.
>
> [ timer:tc(fun() -> [ Pid ! ok || {ok, Pid} <-
> [test_gen:start_link()|| _ <- lists:seq(1,10000)] ] end) || _ <-
> lists:seq(1,10)].
>
> [ timer:tc(fun() -> [ Pid ! ok || Pid <- [spawn_link(fun() -> receive
> _ -> ok end end) || _ <- lists:seq(1,10000)] ] end) || _ <-
> lists:seq(1,10)].
>
>
> First start 10,000 gen_server with a very simple handle_info that
> shuts down the process when it receives anything.  Do this 10 times
> measuring the times for each just to get a bit of smoothing.
> Then do the same with spawn_link.  The idea is that we can compare the
> times and see how much overhead we have for OTP.
>
> The problem is that the spawn_link version takes 4 times LONGER to
> execute!  The issue has to be with the way I do the spawn.
> Can any one please shed some light on this?
>
> -]-[einrich
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list