How fast can Erlang send messages?

September 10th, 2006 tonyg

My previous post examined Erlang’s speed of process setup and teardown. Here I’m looking at how quickly messages can be sent and received within a single Erlang node. Roughly speaking, I’m seeing 3.4 million deliveries per second one-way, and 1.4 million roundtrips per second (2.8 million deliveries per second) in a ping-pong setup in the same environment as previously - a 2.8GHz Pentium 4 with 1MB cache.

Here’s the code I’m using - time_diff and dotimes aren’t shown, because they’re the same as the code in the previous post:

-module(ipctest).
-export([oneway/0, consumer/0, pingpong/0]).

oneway() ->
    N = 10000000,
    Pid = spawn(ipctest, consumer, []),
    Start = erlang:now(),
    dotimes(N - 1, fun () -> Pid ! message end),
    Pid ! {done, self()},
    receive ok -> ok end,
    Stop = erlang:now(),
    N / time_diff(Start, Stop).

pingpong() ->
    N = 10000000,
    Pid = spawn(ipctest, consumer, []),
    Start = erlang:now(),
    Message = {ping, self()},
    dotimes(N, fun () ->
               Pid ! Message,
               receive pong -> ok end
           end),
    Stop = erlang:now(),
    N / time_diff(Start, Stop).

consumer() ->
    receive
    message -> consumer();
    {done, Pid} -> Pid ! ok;
    {ping, Pid} ->
        Pid ! pong,
        consumer()
    end.

%% code omitted - see previous post

Entry Filed under: Technology, Programming, Erlang

2 Comments Add your own

  • 1. matthew  |  September 11th, 2006 at 8:53 am

    funs are more expensive to invoke than normal module methods. Can you try this without using dotimes - make the fun a normal recursive method?

  • 2. tonyg  |  September 11th, 2006 at 11:17 pm

    Removing dotimes for oneway produced a speed boost of about 10% - but doing the same for pingpong scarcely affected the speed at all. If anything, it ran slightly slower. It’s possible that the extra arguments to the helper procedure (Count, Pid, Message) for the non-dotimes pingpong costs more than the overhead of using a fun for the dotimes pingpong. The fun can close over (Pid, Message) once, avoiding the need to pass the parameters around.

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

Calendar

September 2006
M T W T F S S
« Aug   Oct »
 123
45678910
11121314151617
18192021222324
252627282930  

Most Recent Posts