Pattern matching vs. ETS lookups

Vance Shipley <>
Tue Feb 17 07:13:38 CET 2004


Forgot to attach the attachments ...

On Tue, Feb 17, 2004 at 01:01:14AM -0500, Vance Shipley wrote:
}  
}  The attached runs four different versions a gen_server.
-------------- next part --------------
-module(tets).
-export([init/1, handle_call/3, terminate/2]).
-behaviour(gen_server).
 
init(Used) ->
	State = ets:new(mf, [private]),
	ets:insert(State, ttables:make_list(Used)),
   {ok, State}.

handle_call(<<FunctionID:16, Body/binary>>, _From, State) ->
	case catch ets:lookup_element(State, FunctionID, 2) of
		{'EXIT', _Reason} ->
			{reply, undefined, State};
		{M, F} ->
   		{reply, {M, F}, State}
	end.

terminate(Reason, State) -> ok.
 
-------------- next part --------------
-module(ttime).
-export([tlist/2, tlist/1, ttree/2, ttree/1, ttuples/1, tets/2, tets/1]).

tlist(N) -> tlist(N, 16#ffff).
tlist(N, Used) -> go(N, Used, tlist).

ttree(N) -> ttree(N, 16#ffff).
ttree(N, Used) -> go(N, Used, ttree).

ttuples(N) -> go(N, 16#ffff, ttuples).

tets(N) -> tets(N, 16#ffff).
tets(N, Used) -> go(N, Used, tets).


go(N, Used, Module) ->
	{ok, Pid} = gen_server:start_link(Module, Used, []),
	TraceFile = atom_to_list(Module) ++ ".trace",
	ok = fprof:trace(start, TraceFile),
	do(Pid, N, []),
	fprof:trace(stop).

do(Pid, 0, Acc) -> 
	Acc;
do(Pid, N, Acc) ->
	Bin = <<(random:uniform(16#ffff)):16, <<0:256>>/binary>>,
	MF = gen_server:call(Pid, Bin),
	do(Pid, N - 1, Acc ++ [MF]).

-------------- next part --------------
-module(ttree).
-export([init/1, handle_call/3, terminate/2]).
-behaviour(gen_server).
 
init(Used) ->
   {ok, ttables:make_tree(Used)}.

handle_call(<<FunctionID:16, Body/binary>>, _From, State) ->
	case gb_trees:lookup(FunctionID, State) of
		{value, {M, F}} ->
   		{reply, {M, F}, State};
		none ->
			{reply, undefined, State}
	end.

terminate(Reason, State) -> ok.
 
-------------- next part --------------
-module(ttables).
-export([make_list/0, make_list/1, make_tree/0, make_tree/1,
			make_tuple_of_tuples/0]).


make_list() ->
	make_list(16#ffff).
make_list(Used) ->
	make_list(Used, 16#ffff, []).
make_list(Used, -1, Acc) ->
	Acc;
make_list(Used, X, Acc) ->
	case skip(Used) of
		false ->
			make_list(Used, X - 1, [{X, make_tuple()}] ++ Acc);
		true ->
			make_list(Used, X - 1, Acc)
	end.

make_tree() ->
	make_tree(16#ffff).
make_tree(Used) ->
	gb_trees:from_orddict(make_list(Used)).

make_tuple_of_tuples() ->
	make_tuple_of_tuples(16#ff, []).
make_tuple_of_tuples(-1, Acc) ->
	list_to_tuple(Acc);
make_tuple_of_tuples(X, Acc) ->
	T = make_tuples(16#ff, []),
	make_tuple_of_tuples(X -1, [T] ++ Acc).

make_tuples(-1, Acc) ->
	list_to_tuple(Acc);
make_tuples(X, Acc) ->
	make_tuples(X -1, [make_tuple()] ++ Acc).

make_tuple() ->
	{make_atom(), make_atom()}.
	
make_atom() ->
	Length = random:uniform(20) + 5,
	make_atom(Length, []).
make_atom(0, Acc) ->
	list_to_atom(Acc);
make_atom(X, Acc) ->
	C = random:uniform(26) + 96,
	make_atom(X - 1, Acc ++ [C]).

skip(Used) ->
	case random:uniform(round(16#ffff/Used)) of
		1 -> false;
		_ -> true
	end.
-------------- next part --------------
-module(tlist).
-export([init/1, handle_call/3, terminate/2]).
-behaviour(gen_server).
 
init(Used) ->
   {ok, ttables:make_list(Used)}.

handle_call(<<FunctionID:16, Body/binary>>, _From, State) ->
	case lists:keysearch(FunctionID, 1, State) of
		{value, {M, F}} ->
   		{reply, {M, F}, State};
		false ->
			{reply, undefine, State}
	end.

terminate(Reason, State) -> ok.
 
-------------- next part --------------
-module(ttuples).
-export([init/1, handle_call/3, terminate/2]).
-behaviour(gen_server).
 
init(_) ->
   {ok, ttables:make_tuple_of_tuples()}.

handle_call(<<FunID_MSB:8, FunID_LSB:8, Body/binary>>, _From, State) ->
	{M, F} = element(FunID_LSB + 1, element(FunID_MSB + 1, State)),
   {reply, {M, F}, State};

handle_call({stop, Reason}, _From, State) ->
   {stop, Reason, State}.

terminate(Reason, State) -> ok.
 


More information about the erlang-questions mailing list