10
votes

I am build a very large dictionary and I am performing many checks to see if a key is in the structure and then adding if it unique or incrementing a counter if it is identical.

Python uses a hash data structure to store dictionaries (not to be confused with a cryptographic hash function). Lookups are O(1), but if the hash table is full the it has to be rehashed which is very expensive.

My Question is, would I be better off using a AVL Binary Search Tree or is a hash table good enough?

5
Note that if you're at the point of wondering which has better performance between a hashtable and an AVL tree, there are plenty of other players to consider. Trie and splay tree spring to mind. - Steve Jessop
Maybe you can even use a counting bloom filter. - Jochen Ritzel
from collections import Counter - Matthieu M.
@THC4k yeah google uses a bloom filter, interesting stuff. - rook

5 Answers

26
votes

The only way to be sure would be to implement both and check, but my informed guess is that the dictionary will be faster, because a binary search tree has cost O(log(n)) for lookup and insertion, and I think that except under the most pessimal of situations (such as massive hash collisions) the hash table's O(1) lookup will outweigh the occasional resize.

If you take a look at the Python dictionary implementation, you'll see that:

  1. a dictionary starts out with 8 entries (PyDict_MINSIZE);
  2. a dictionary with 50,000 or fewer entries quadruples in size when it grows;
  3. a dictionary with more than 50,000 entries doubles in size when it grows;
  4. key hashes are cached in the dictionary, so they are not recomputed when the dictionary is resized.

(The "NOTES ON OPTIMIZING DICTIONARIES" are worth reading too.)

So if your dictionary has 1,000,000 entries, I believe that it will be resized eleven times (8 → 32 → 128 → 512 → 2048 → 8192 → 32768 → 131072 → 262144 → 524288 → 1048576 → 2097152) at a cost of 2,009,768 extra insertions during the resizes. This seems likely to be much less than the cost of all the rebalancing involved in 1,000,000 insertions into an AVL tree.

4
votes

What's the ratio of items vs unique items? What's the expected number of unique items?

If a hash bucket fills, then extending should just be a matter of some memory reallocation, not rehashing.

Testing a counting dict should be very quick and easy to do.

Note also the counter class available since python 2.7 http://docs.python.org/library/collections.html#counter-objects http://svn.python.org/view?view=rev&revision=68559

4
votes

Python dictionaries are highly optimized. Python makes various special-case optimizations that the Python developers cater for in the CPython dictionary implementation.

  1. In CPython, all PyDictObject's are optimized for dictionaries containing only string keys.
  2. Python's dictionary makes an effort to never be more than 2/3rds full.

The book "Beautiful Code" discusses this all.

The eighteenth chapter is Python's Dictionary Implementation: Being All Things to All People by Adrew Kuchling

It is much better to use it than try to achieve the hand crafted custom implementation which will have to replicate all these optimizations to be any where near the main CPython implementation of dictionary look ups.

2
votes

You would have to implement your own data structures in C to stand a reasonable chance of beating the built-in structures.

Also you can avoid some of the overhead by using get, avoiding find existing elements twice. Or collections.Counter if you are using python 2.7+.

def increment(map, key):
    map[key] = map.get(key,0)+1
2
votes

Using a dict is O(1). As the dict grows, reallocation is sometimes required, but that is amortized O(1)

If your other algorithm is O(log n), the simple dict will always beat it as the dataset grows larger.

If you use any type of tree, I would expect a O(log n) component in there somewhere.

Not only is a hash table good enough, it is better