[erlang-questions] Benchmarking Erlang: Deathmatch of gb_trees, dict, ets, mnesia ... and registered names
Joel Reymont
joelr1@REDACTED
Thu Oct 9 13:09:26 CEST 2008
%%% dict
-module(map2).
-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(map2, [], []).
init([]) ->
{ok, #data{ xref = dict:new() }}.
terminate(_, _) ->
ok.
handle_cast({'ADD', Key, Val}, Data) ->
Xref = Data#data.xref,
{noreply, Data#data{ xref = dict:store(Key, Val, Xref) }};
handle_cast(stop, Data) ->
{stop, normal, Data}.
handle_call({'LOOKUP', Key}, _From, Data) ->
{reply, dict:fetch(Key, Data#data.xref), 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(map2, populate, [Pid, N]),
{T2, _} = timer:tc(map2, 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