Very fast indeed.

1> spawntest:serial_spawn(1).

3.58599e+5

That’s telling me that Erlang can create and tear down processes at a

rate of roughly 350,000 Hz. The numbers change slightly – things slow

down – if I’m running the test in parallel:

2> spawntest:serial_spawn(10).

3.48489e+5

3> spawntest:serial_spawn(10).

3.40288e+5

4> spawntest:serial_spawn(100).

3.35983e+5

5> spawntest:serial_spawn(100).

3.36743e+5

[Update: I forgot to mention earlier that the system seems to spend 50% CPU in user and 50% in system time. Very odd! I wonder what the Erlang runtime is doing to spend so much system time?]

Here’s the code for what I’m doing:

-module(spawntest).

-export([serial_spawn/1]).

serial_spawn(M) ->

N = 1000000,

NpM = N div M,

Start = erlang:now(),

dotimes(M, fun () -> serial_spawn(self(), NpM) end),

dotimes(M, fun () -> receive X -> X end end),

Stop = erlang:now(),

(NpM * M) / time_diff(Start, Stop).

serial_spawn(Who, 0) -> Who ! done;

serial_spawn(Who, Count) ->

spawn(fun () ->

serial_spawn(Who, Count – 1)

end).

dotimes(0, _) -> done;

dotimes(N, F) ->

F(),

dotimes(N – 1, F).

time_diff({A1,A2,A3}, {B1,B2,B3}) ->

(B1 – A1) * 1000000 + (B2 – A2) + (B3 – A3) / 1000000.0 .

This is all on an Intel Pentium 4 running at 2.8GHz, with 1MB cache, on Debian linux, with erlang_11.b.0-3_all.deb.