[erlang-questions] Benchmarking Erlang: Deathmatch of gb_trees, dict, ets, mnesia ... and registered names

Joel Reymont joelr1@REDACTED
Thu Oct 9 13:14:12 CEST 2008


%%% and finally, the moment you have all been waiting for!
%%% ...
%%% registered names

-module(map7).
-behaviour(gen_server).

-export([init/1, handle_call/3, handle_cast/2,
	 handle_info/2, terminate/2, code_change/3]).

-export([start/1, test/1, populate/1, lookup/1]).

start(N) ->
     gen_server:start({global, N}, map7, [], []).

init([]) ->
     {ok, none}.

terminate(_, _) ->
     ok.

handle_cast(stop, Data) ->
     {stop, normal, Data}.

handle_call(Event, From, Data) ->
     error_logger:info_report([{module, ?MODULE},
			      {line, ?LINE},
			      {self, self()},
                               {event, Event},
                               {from, From}
                              ]),
     {noreply, 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) ->
     {T1, _} = timer:tc(map7, populate, [N]),
     {T2, _} = timer:tc(map7, lookup, [N]),
     io:format("Populate: ~.4. f~n", [T1 / 1000000]),
     io:format("Lookup:   ~.4. f~n", [T2 / 1000000]),
     stop(N).

populate(0) ->
     ok;

populate(N) ->
     {ok, _} = start(N),
     populate(N - 1).

lookup(0) ->
     ok;

lookup(N) ->
     P = global:whereis_name(N),
     lookup(N - 1).

stop(0) ->
     ok;

stop(N) ->
     gen_server:cast({global, N}, stop),
     stop(N - 1).




More information about the erlang-questions mailing list