[erlang-questions] what's happened in the shootout

Ulf Wiger (TN/EAB) <>
Mon Jun 18 20:50:17 CEST 2007


Klacke wrote:
>
> We already have such an efficent BIF:
> 
> -module(out).
> -compile(export_all).
> 
> outp() ->
>      P = open_port({fd, 1, 1}, [out]).
> 
> test() ->
>      P = outp(),
>      port_command(P, "Hi there \r\n"),
>      port_command(P, <<"Hi there again\r\n">>),
>      port_command(P, ["And ", <<"hello">>, "again", "\r\n"]).

Yeah, I noticed after some experimentation.

Do you think programs like this one would be
accepted in the shootout?

-module(sumcol).
-export([main/1]).
-export([start/0]).

%% get the program argument, which is how many test iterations to run
%% for this test, we ignore this parameter
main(_Args) ->
    user ! {self(), wait_ready},
    receive
	ready ->
	    halt()
    end.

%% callback function for custom user process
start() ->
    spawn(fun() ->
		  process_flag(priority,high),
		  register(user, self()),
		  read_in(open_port({fd,0,1}, [{line,80},eof]), 0)
	  end).

read_in(Port, Sum) ->
    receive
	{Port, {data, {Eol, Str}}} when Eol==eol; Eol==noeol->
          read_in(Port, Sum + list_to_integer(Str));
	{Port, eof}  ->
	    ready(Port, Sum)
    end.

ready(Port, Sum) ->
    port_command(Port, [integer_to_list(Sum), "\n"]),
    erlang:port_close(Port),
    receive
	{From, wait_ready} ->
	     From ! ready
    end.


It runs circles around the posted version that uses
the io module, _and_ uses less memory.

(Original)
ws73032> time erl -boot start_clean -noshell -run sumcol0 main 1000 < sum-in.txt
2907096
3.55u 0.18s 0:03.82 97.6%

(My version)
ws73032> time erl -boot start_clean -user sumcol -run sumcol main 1000 < sum-in.txt
2907096
0.34u 0.11s 0:00.63 71.4%


It does say that "Programs should use built-in line-oriented I/O functions rather than custom-code", but arguably, this program does exactly that.


/Uffe
 

> -----Original Message-----
> From: Claes Wikström [mailto:] 
> Sent: den 18 juni 2007 20:27
> To: Ulf Wiger (TN/EAB)
> Cc: Bengt Kleberg (TN/EAB); Erlang Questions
> Subject: Re: [erlang-questions] what's happened in the shootout
> 
> Ulf Wiger (TN/EAB) wrote:
> 
> > 
> > Couldn't we introduce a BIF, erlang:put_chars(IoList), (or possibly 
> > put_chars(stdout | stderr, IoList)) which simply writes the given 
> > bytes to stdout?
> > 
> 
> We already have such an efficent BIF:
> 
> -module(out).
> -compile(export_all).
> 
> outp() ->
>      P = open_port({fd, 1, 1}, [out]).
> 
> test() ->
>      P = outp(),
>      port_command(P, "Hi there \r\n"),
>      port_command(P, <<"Hi there again\r\n">>),
>      port_command(P, ["And ", <<"hello">>, "again", "\r\n"]).
> 
> 
> 
> /klacke
> 



More information about the erlang-questions mailing list