[erlang-questions] dict slower than ets?

293 views
Skip to first unread message

Wanglei

unread,
Aug 23, 2009, 12:18:25 PM8/23/09
to erlang-questions
dict write x 10000 

> {T,D} = timer:tc(test,dict_read,[]).
{86177,...

dict read x 10000 
> timer:tc(test,dict_read,[D]).
{17260,

-----------------------------

ets write x 10000 
> {T,E}=timer:tc(test,ets_write,[]).
{18005,20493}

dict read x 10000 
> timer:tc(test,ets_read,[E]).      
{15706,


test.erl
---------- 8< ------------------

-module(test).
-export([dict_write/0,dict_read/1,ets_write/0,ets_read/1]).

dict_write()->
D = dict:new(),
dict_write(D,10000).

dict_write(D,0)->D;
dict_write(D,N)->dict_write(dict:store(N,N,D),N-1).

dict_read(D)->
[ dict:find(X,D) || X<-lists:seq(0,10000) ].

ets_write()->
E = ets:new(test,[set]),
[ ets:insert(E,{X,X}) || X<-lists:seq(0,10000) ],
E.

ets_read(E)->
[ ets:lookup(E,X) || X<-lists:seq(0,10000) ].

test.erl

Colm Dougan

unread,
Aug 23, 2009, 7:16:33 PM8/23/09
to Wanglei, erlang-questions
I'm pretty sure it would be expected that dict would be slower than
ets for this use-case. All the ets calls and data structures are
implemented directly in C while the 'dict' modules uses pure erlang to
emulate similar functionality. If your keys are integers you could
also look at the 'array' module which uses element/setelement under
the hood rather than hashing.

Colm

> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Ngoc Dao

unread,
Aug 23, 2009, 8:41:22 PM8/23/09
to Colm Dougan, Wanglei, erlang-questions
FYI: http://d.hatena.ne.jp/cooldaemon/20080722/1216706154

On my laptop:
benchmark:run(1000000).
--<process dictionary>--
set:210(302)ms
get:190(283)ms
--<dict>--
set:59430(62403)ms
get:790(841)ms
--<ets>--
set:1470(1566)ms
get:940(984)ms
--<gb_trees>--
set:10340(11286)ms
get:690(763)ms

Ulf Wiger

unread,
Aug 24, 2009, 3:33:48 AM8/24/09
to Wanglei, erlang-questions
Wanglei wrote:
> dict write x 10000
>
> > {T,D} = timer:tc(test,dict_read,[]).
> {86177,...
>
> dict read x 10000
> > timer:tc(test,dict_read,[D]).
> {17260,
>[...]

Yes, for small objects, dict is slower than ets.
For larger objects, the copying overhead of using ets
will tip the scales in dict's favor* - at least for a non-
shared dictionary. If you need concurrent access to the
dictionary, you need to keep the dict in a separate process,
which will incur the same copying cost as for ets.


* While both solutions use constant-time algorithms,
dict will also suffer from garbage collection overhead,
which is roughly proportional to the size of the live data
set. Ets tables are not garbage collected.

BR,
Ulf W
--
Ulf Wiger
CTO, Erlang Training & Consulting Ltd
http://www.erlang-consulting.com

Reply all
Reply to author
Forward
0 new messages