[erlang-questions] OTP process startup vs spawn overhead
Heinrich Venter
hfventer@REDACTED
Thu Jul 28 13:04:42 CEST 2011
Fun = fun() -> [ Pid ! ok || _ <- lists:seq(1,10000), Pid <-
spawn_link(?MODULE, recv, []) ] end.
This wont work, because spawn_link does not generate a list :)
> Are you running that expression from the shell?
I was executing the tests from the shell yes, but if I do the
following with a compiled function
[ timer:tc(fun() -> [ Pid ! ok || Pid <- [spawn(fun() ->
test_gen:loop() end) || _ <- lists:seq(1,10000)] ] end) || _ <-
lists:seq(1,10)].
I still get the same slow performance. It must be related to creation
of the fun then.
This gets better results
[ timer:tc(fun() -> [ Pid ! ok || Pid <- [spawn(test_gen, loop, []) ||
_ <- lists:seq(1,10000)] ] end) || _ <- lists:seq(1,10)].
It also shows a bout 45% performance penalty for spawning OTP
processes like mad instead of using spawn(Module, Function, Args).
On the other hand using spawn(Fun) seems to be 4 times slower. This
is more or less in line with what the efficiency guide says.
It pays to measure :)
Moral of the story: Spawn your workers with spawn(M,F,A) or use OTP
processes if you can afford the slight performance hit and gain a lot
of convenience.
-]-[einrich
More information about the erlang-questions
mailing list