[erlang-questions] dict, orddict and gb_tree
Fri Feb 9 16:11:58 CET 2007
Ludovic Coquelle wrote:
> Any resources/advices to choose between usage of dict, orddict and gb_tree?
> In my understanding, they are all doing the same thing: "giving a key,
> retrieve the value"; am I right?
> If so, how to choose? I guess trees are better if there is few updates
> (less than reads). And what about dict and orddict? What about any
> criteria relative to the amount of data?
gb_trees is slightly more lightweight and a little bit faster on most
operations than dict, so they are particularly good if you have large
numbers of moderately sized tables. But in general, you're not likely
to notice any difference until you really start to push the limits, so
for everyday programs, you can use either without loss.
However, dict does hashing on the keys (which gb_trees does not), so
if you have complex, large-ish keys (e.g. arbitrary strings), then dict
is a better choice since it doesn't spend as much time comparing keys.
gb_trees assumes that key comparison is a fast, constant time operation.
orddict can be used when there is a real point with having explicit
ordered lists of pairs; e.g, if you do repeated merging but few or no
lookups or inserts. It can be a good idea to convert back and forth
between ordinary dicts/trees and orddicts depending on the phase of
the program; e.g., first building a large orddict by merging many many
smaller ones, and then transforming it to a dict just before you start
to do lookups. But remember that this is optimization, and should not
be done prematurely.
More information about the erlang-questions