当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - .net dictionary vs other managed custom data structures, why is the .net dictionary so fast?

问题描述:

This question already has an answer here:

  • C# Binary Trees and Dictionaries

    5 answers

网友答案:

Two thoughts:

  1. You should make sure you are not inadvertently including JIT time in your tests - this can add a considerable amount of time to the result. You should perform two runs in the same execution and discard the first run.

  2. You should make sure that you are not running under the debugger - this can dramatically skew performance results.

Aside form that, any performance differences you see may very well be the result of the difference in performance between a hash table and a tree. A tree structure typically has O(n*log(n)) performance on average for a lookup. A balanced tree can reduce that to O(lon(n)). Hashtables, meanwhile, can approach O(1) time for lookups when hash collisions are avoided.

I would also imagine that the .NET Dictionary class is highly optimized since it is a bread-and-butter data structure for so many different things in .NET. Also, a generic Dictionary<> may be able to avoid boxing, and therefore you may see some performance differences from that.

网友答案:

If all you need is a lookup, a red/black tree will not be your best data structure. It provides sorting, which is always going to be slower than a hashtable lookup. If you want to compare .net Dictionary with a comparable C5 data structure, you would use C5.HashDictionary.

网友答案:

Choose the data structure and repository depending on the data. That said, there is no perfect data structure. While the .NET Dictionary<,> is well optimized because it is often a good choice, it is not the answer to all problems - that would be 42...

分享给朋友:
您可能感兴趣的文章:
随机阅读: