The Great Computer Language Shootout is back

Vance Shipley vances@REDACTED
Fri Jun 25 19:27:56 CEST 2004


I didn't like the exception implementation so I rewrote it.
It had been using the process dictionary and was doing throws.
I rewrote it to handle real exceptions and not use the process
dictionary.  I managed to slow it down. :)

	-Vance


$ time erl -noinput -s except main 200000
Exceptions: HI=100000 / LO=100000

real    0m0.927s
user    0m0.650s
sys     0m0.150s

$ time erl -noinput -run except2 main 200000
Calling: 100000, Caller: 100000

real    0m0.994s
user    0m0.730s
sys     0m0.140s


-------------- next part --------------
%% The Great Computer Language Shootout
%% contributed by Isaac Gouy (Erlang novice)
%%
%% http://shootout.alioth.debian.org/
%%
%% Used destructive assignment in the process dictionary 
%% to keep count of handled exceptions.
%%
%% Usage: start from command line with
%%     erlc except.erl
%%     erl -noinput -s except main 200000

-module(except). 
-export([main/1]). 


blowup(N) when N rem 2 == 0 -> throw({lo_exception, N});
blowup(N) -> throw({hi_exception, N}).


lo_fun(N) ->
    case catch blowup(N) of
        {lo_exception, N1} -> put(lo_count, get(lo_count) + 1);
        {hi_exception, N2} -> throw({hi_exception, N2})
    end.


hi_fun(N) -> 
    case catch lo_fun(N) of
        {hi_exception, N1} -> put(hi_count, get(hi_count) + 1);
        _ -> true
    end.


some_fun(0) -> true;
some_fun(N) ->
    case catch hi_fun(N) of
        {lo_exception, N1} -> io:fwrite("~s~n", ["lo_exception should not get here."]);
        {hi_exception, N2} -> io:fwrite("~s~n", ["hi_exception should not get here."]);
        _ -> true
    end,
    some_fun(N - 1).


main([Arg]) ->
    Num = list_to_integer(atom_to_list(Arg)),
    put(hi_count, 0),
    put(lo_count, 0),
    some_fun(Num),
    io:fwrite("~s~w ~s~w~n", ["Exceptions: HI=", get(hi_count),"/ LO=", get(lo_count)]),
    halt(0).

-------------- next part --------------
%% The Great Computer Language Shootout
%% contributed by Vance Shipley
%%
%% http://shootout.alioth.debian.org/
%%
%% Usage: start from command line with
%%     erlc except.erl
%%     erl -noinput -run except main 200000

-module(except2). 
-export([main/1]). 

main([Arg]) ->
	N = list_to_integer(Arg),
	{Calling, Caller} = loop(N, 0, 0),
	io:fwrite("Calling: ~w, Caller: ~w~n", [Calling, Caller]),
	halt(0).

loop(0, Calling, Caller) ->
	{Calling, Caller};
loop(N, Calling, Caller) ->
	case catch call(N, Calling) of
		{'EXIT', _Reason} ->
			%% handled exception here
			loop(N - 1, Calling, Caller + 1);
		X -> 
			%% calling function handled exception
			loop(N - 1, X, Caller)
	end.

call(N, Calling) ->
	%% when do/1 returns ok exits with badmatch
	case catch do(N) of
		{'EXIT', _Reason} ->
			Calling + 1
	end.

%% calling with an odd value exits with badarg
do(N) when N rem 2 == 0 ->
	ok.


More information about the erlang-questions mailing list