[erlang-questions] OTP process startup vs spawn overhead

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Thu Jul 28 13:17:47 CEST 2011


On Thu, Jul 28, 2011 at 13:04, Heinrich Venter <hfventer@REDACTED> wrote:
>
> [ 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.

There are a couple of problems here.

* We generate a list (lists:seq(1,10000)) inside the shell and inside
timer:tc/1. This time is also measured against us.
* We create a fun in the shell which is then interpreted.

Try (not tested)

-module(foo).
-compile(export_all).

test() ->
  L = lists:seq(1, 10000),
  F = fun() -> test_gen:loop() end, % this can be "eta"-converted to
'fun test_gen:loop/0' but oh well.
  [timer:tc([spawn(F) || _ <- L]) || _ <- lists:seq(1, 10)].

or similar. And then call this from the shell. Also do the same for
the one below.

>
> This gets better results
> [ timer:tc(fun() -> [ Pid ! ok || Pid <- [spawn(test_gen, loop, []) ||
> _ <- lists:seq(1,10000)] ] end) || _ <- lists:seq(1,10)].

This guy doesn't create the fun in the shell, so I expect it to be better.

> 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.

I wouldn't be so sure :) It also worth it to ask erlc to output the
BEAM bytecode and look at it. I have a hunch the shell is still
haunting your measurements.

-- 
J.



More information about the erlang-questions mailing list