How fast can Erlang create processes?

By: on September 10, 2006

Very fast indeed.

1> spawntest:serial_spawn(1).

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> spawntest:serial_spawn(10).

4> spawntest:serial_spawn(100).
5> spawntest:serial_spawn(100).

[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:


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)

dotimes(0, _) -> done;
dotimes(N, 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.



  1. matthew says:

    My guess is that the kernel time is syscalls to malloc and free for the allocation and deallocation of stack and heap for each process.

  2. Paul Crowley says:

    malloc and free aren’t syscalls – malloc calls sbrk or similar if it needs more memory, but otherwise it manages the heap in userspace. It should have a certain amount of hysteresis before it tries to give any memory back to kernel space, so all the malloc and free stuff should be happening entirely in userspace.

    Of course being a garbage-collected language it won’t be calling malloc and free – it’ll be directly calling sbrk or similar (mmap in fact AIUI) and managing the memory returned itself.

  3. SM Smithfield says:

    Just ran your test on a Mac (10.4 G5 2.1Ghz 512kB cache)
    7> c(spawntest).
    8> spawntest:serialspawn(1).
    9> spawntest:serial
    10> spawntest:serialspawn(10).
    11> spawntest:serial
    12> spawntest:serial_spawn(100).

    I think these fellows might be onto something.

  4. ratatask says:

    strace the erlang process if you want to see what it’s doing using
    system time.
    I wouldn’t be surprised if it was time reading, or a select/poll loop 😐

  5. tonyg says:

    @ratatask: yes, that’d probably give some idea. I don’t just yet fancy the job of trawling through the megabytes of strace output looking for patterns, though 🙂

  6. It does make a lot of poll() call, but also much much more times() and gettimeofday() calls (700 times more than poll()).

  7. def ZA says:

    “I don’t just yet fancy the job of trawling through the megabytes of strace output looking for patterns, ”

    You could maybe use the summary flag on strace to “strace -c” to give you a breakdown.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>