The Great Computer Language Shootout is back

Mark Scandariato mscandar@REDACTED
Wed Jul 28 17:23:10 CEST 2004


(One post to 'shootout-list@REDACTED' and I start getting 
Nigerian letters!)

Try this one. Seems about the same speed as the other version.
I tried it on a 4 MB test file (on a 2.4GHz P4, Redhat 9) and it took about 1
second, exclusive of erlang's startup time. (The native wc took about .67s).

Mark.
-----------------------

-module(wc).

-export([start/0]).

start() ->
     % io:fwrite("~p~n", [now()]),
     ok = io:setopts([binary]),
     s(next({0, <<>>}), 0, 0).

next({C, <<Char:8, Rest/binary>>}) ->
     {Char, {C+1, Rest}};

next({C, <<>>}) ->
     case io:get_chars('', 4096) of
         eof -> {eof, C};
         Bin -> next({C, Bin})
     end.

done(L, W, C) ->
     io:fwrite("~p\t~p\t~p~n", [L, W, C]),
     % io:fwrite("~p~n", [now()]),
     erlang:halt().

s({eof, C}, L, W) -> done(L, W, C);
s({$\s, S}, L, W) -> s(next(S), L, W);
s({$\t, S}, L, W) -> s(next(S), L, W);
s({$\n, S}, L, W) -> s(next(S), L+1, W);
s({_, S}, L, W)   -> w(next(S), L, W+1).

w({eof, C}, L, W) -> done(L, W, C);
w({$\s, S}, L, W) -> s(next(S), L, W);
w({$\t, S}, L, W) -> s(next(S), L, W);
w({$\n, S}, L, W) -> s(next(S), L+1, W);
w({_, S}, L, W)   -> w(next(S), L, W).

------------

Isaac Gouy wrote:

> One of the peculiarities of the Shootout tests is that they take
> redirected input from the commandline rather than opening a file
> directly. How would you do that?
> 
> --- Mark Scandariato <mscandar@REDACTED> wrote:
> 
>>How about:
>>
>>-module(wc).
>>-compile(export_all).
>>
>>start(File) ->
>>     {ok, F} = file:open(File, [raw, read, binary]),
>>     s(next({F, 0, <<>>}), 0, 0).
>>
>>next({F, C, <<Char:8, Rest/binary>>}) ->
>>     {Char, {F, C+1, Rest}};
>>
>>next({F, C, <<>>}) ->
>>     case file:read(F, 4096) of
>>         {ok, Bin} -> next({F, C, Bin});
>>         eof -> {eof, C}
>>     end.
>>
>>s({eof, C}, L, W) -> {L, W, C};
>>s({$\s, S}, L, W) -> s(next(S), L, W);
>>s({$\t, S}, L, W) -> s(next(S), L, W);
>>s({$\n, S}, L, W) -> s(next(S), L+1, W);
>>s({_, S}, L, W)   -> w(next(S), L, W+1).
>>
>>w({eof, C}, L, W) -> {L, W, C};
>>w({$\s, S}, L, W) -> s(next(S), L, W);
>>w({$\t, S}, L, W) -> s(next(S), L, W);
>>w({$\n, S}, L, W) -> s(next(S), L+1, W);
>>w({_, S}, L, W)   -> w(next(S), L, W).
> 
> 
> 
> 
> 		
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail - 50x more storage than other providers!
> http://promotions.yahoo.com/new_mail



More information about the erlang-questions mailing list