[erlang-questions] Erlang port speed

Patrick Mahoney <>
Sat May 30 00:53:31 CEST 2009


On Fri, May 29, 2009 at 12:04:46PM -0400, Colin Z wrote:
> 
> Ping-ponging 1 million times takes about 160 seconds between Erlang and C# .
> I'm benchmarking using the statistics module. I'm not doing any io:fwrites,
> etc.
> 
> Between the two C# processes it takes about 15 seconds.
> 

I wrote a crummy benchmark that sends "ping" and "pong" back and forth
under Linux using both a C program and an Erlang program to drive the
same "simple_port.c" port.  My Erlang program is much closer to C than
your example, but is still about 30% slower for 1 million ping/pong.

Here's the result with a C program controling "simple_port.c":

$ time ./port_control
ping/pong 1000000 times

real    0m16.570s
user    0m11.017s
sys     0m5.188s

And here's the result of an erlang program controlling "simple_port.c":

Erlang R13A (erts-5.7) [source] [smp:2:2] [rq:2] [async-threads:0]
[hipe] [kernel-poll:false]

Eshell V5.7  (abort with ^G)
1> c(port_control).
{ok,port_control}
2> port_control:test().
ping/poing 1000000 times
20.681763s
ok
3> port_control:test().
ping/poing 1000000 times
21.058769s
ok

I don't have time to test on a win32 machine (and the C control program
would have to to significantly changed to compile under win32;
erts/emulator/sys/win32/sys.c might be helpful here).

I recall a long time ago (unrelated to erlang) hearing about poor pipe
performance in win32.  I also notice that the "gs" gui library included
in the Erlang source, specifically gstk_port_handler.erl and gstk.tcl,
uses a socket rather than stdin/stdout pipes when run in windows.  I
don't see any obvious comments, but I wonder if socket performance is
much better than pipes under win32.

If this is indeed the reason, then perhaps the documentation should
mention it and possibly provide an example using sockets.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: port_common.c
Type: text/x-csrc
Size: 1205 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090529/299606ef/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: port_common.h
Type: text/x-chdr
Size: 155 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090529/299606ef/attachment-0001.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: port_control.c
Type: text/x-csrc
Size: 1021 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090529/299606ef/attachment-0002.bin>
-------------- next part --------------
-module(port_control).

-define(N_MSGS, 1000000).

-compile(export_all).

test() ->
    {MicroSec, ok} = timer:tc(?MODULE, run, []),
    io:format("~ps~n", [MicroSec/1000000]).

run() ->
    Port = open_port({spawn, "./simple_port"},
		     [{packet, 1}, binary]),
    loop(Port, ?N_MSGS).

loop(Port, 0) ->
    port_command(Port, <<"stop\0">>),
    port_close(Port),
    io:format("ping/poing ~p times~n", [?N_MSGS]);
loop(Port, N) ->
    port_command(Port, <<"ping\0">>),
    receive
	{Port, {data, <<"pong\0">>}} ->
	    loop(Port, N-1);
	Err ->
	    exit({unexpected_msg, Err})
    after
	500 ->
	    timeout
    end.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: simple_port.c
Type: text/x-csrc
Size: 283 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090529/299606ef/attachment-0003.bin>


More information about the erlang-questions mailing list