[erlang-questions] Benchmarking Erlang: Deathmatch of gb_trees, dict, ets, mnesia ... and registered names
Joel Reymont
joelr1@REDACTED
Thu Oct 9 13:10:05 CEST 2008
%%% ets
-module(map3).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3]).
-export([start/0, test/1, populate/2, lookup/2]).
-record(data, {
xref
}).
start() ->
gen_server:start(map3, [], []).
init([]) ->
{ok, #data{ xref = ets:new(xref, []) }}.
terminate(_, _) ->
ok.
handle_cast({'ADD', Key, Val}, Data) ->
ets:insert(Data#data.xref, [{Key, Val}]),
{noreply, Data};
handle_cast(stop, Data) ->
{stop, normal, Data}.
handle_call({'LOOKUP', Key}, _From, Data) ->
[Value] = ets:lookup(Data#data.xref, Key),
{reply, Value, Data}.
handle_info(Info, Data) ->
error_logger:info_report([{module, ?MODULE},
{line, ?LINE},
{self, self()},
{message, Info}]),
{noreply, Data}.
code_change(_, Data, _) ->
{ok, Data}.
test(N) ->
{ok, Pid} = start(),
{T1, _} = timer:tc(map3, populate, [Pid, N]),
{T2, _} = timer:tc(map3, lookup, [Pid, N]),
io:format("Populate: ~.4. f~n", [T1 / 1000000]),
io:format("Lookup: ~.4. f~n", [T2 / 1000000]),
gen_server:cast(Pid, stop).
populate(_, 0) ->
ok;
populate(Pid, N) ->
gen_server:cast(Pid, {'ADD', N, N}),
populate(Pid, N - 1).
lookup(_, 0) ->
ok;
lookup(Pid, N) ->
gen_server:call(Pid, {'LOOKUP', N}),
lookup(Pid, N - 1).
More information about the erlang-questions
mailing list