[erlang-questions] Process scope variable

Fri Feb 20 11:20:22 CET 2015

>> Threading state through: 10 msec.
> Using a state process that was as lightweight as possible: 1670 msec.
>> This wasn't native compiled, and it didn't do any real work, so I
> don't expect real code to be 167 times slower using a state process.
> But I do expect it to be a lot slower.
> thank you very much for running this test! appreciate this.

I've now extended the measurements, on a different machine.

%% All measurements are milliseconds for 10,000,000 iterations of
%% a loop counting down.

%% c(foo).      c(foo, [native]).       method
%%   140           50                   pass a number around
%%   380          100                   pass {V,N}, build new N
%%   630          410                   process dictionary, atom key
%%  9260         9150                   other process, use dec
%% 16310        16010                   state held in other process,
                                        get/put two messages each.

The relevant ratio would seem to be 16010/100 = 160.1

16010 ms for 10,000,000 iterations is 1.601 sec for 1,000,000
iterations, or 1.6 µsec for 1 iteration.  Now each iteration
does four message sends, so that's 0.4 µsec per !+receive.

Stripped to the bone, using 'dec' instead of {put,_,N}, so that
each iteration does three message sends, for 0.3 µsec per !+receive.
The faster state process used this loop:

state_loop2(N, Pid) ->
        get -> Pid!N, state_loop2(N, Pid)
      ; dec -> state_loop2(update(N), Pid)
      ; eof -> ok

to keep the messages as small as possible and to eliminate a
reply for 'dec'.

I can see no advantage to putting state in another process
compared with putting state in the process dictionary.

More information about the erlang-questions mailing list