Why is Dictionary.First() so slow?

前端 未结 5 1114
自闭症患者
自闭症患者 2020-12-11 16:35

Not a real question because I already found out the answer, but still interesting thing.

I always thought that hash table is the fastest associative container if you

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 17:12

    Dictionary makes no effort to keep track of a list of keys. So the iterator needs to walk the buckets. Many of these buckets, particularly for a large dictionary, many not have anything in them.

    It may be helpful to compare OpenJDK's HashIterator.nextEntry and PrivateEntryIterator.nextEntry (which uses TreeMap.successor). The hash version walks an unknown number of entries looking for one that's non-null. This could be particularly slow if the hash table has had many elements removed (which it has in your case). In TreeMap, the only walking we do is our in-order traversal. There are no nulls in the way (only at the leaves).

提交回复
热议问题