I. Dictionary and Hashtable
both dictionary and Hashtable are dictionary classes in the. Net framework and can be based on Key Quick Find value
The performance of the dictionary depends on the implementation code of the GetHashCode () method of the key type.
The key type must also implement IEQUATABLE<T>. The Equals () method, and if A.equals (b) Returns True, the GetHashCode () of A and B must also return the same value.
Dictionary
- For multithreading
- has a generic advantage (type-safe, better performance), and for value types, there is no performance loss for boxing and unboxing
- Fast read speed (reflected on a single piece of data)
- Fuller Capacity Utilization
- Ordered
Hashtable
- Full thread-safe type is obtained by the static method synchronize method
- Disordered
Two. List and dictionary
The list is somewhat similar to dictionary. both have the advantage of using generics, and dictionary does not have the performance overhead of moving subsequent elements in memory.
List traversal query faster (when data is large), dictionary single query faster
To cite an analysis of an article:
同样是集合,为什么性能会有这样的差距。我们要从存储结构和操作系统的原理谈起。
首先我们清楚List<T>是对数组做了一层包装,我们在数据结构上称之为线性表,而线性表的概念是,在内存中的连续区域,除了首节点和尾节点外,每个节点都有着其唯一的前驱结点和后续节点。我们在这里关注的是连续这个概念。
而HashTable或者Dictionary,他是根据Key而根据Hash算法分析产生的内存地址,因此在宏观上是不连续的,虽然微软对其算法也进行了很大的优化。 because of this discontinuity, in the traversal, dictionary will inevitably produce a large number of memory paging operations, and list only need to make a minimum of memory page, this is the list and dictionary in the time of the difference between the root cause of the efficiency.
6. 再谈Dictionary
也许很多人说,既然Dictionary如此强大,那么我们为什么不用Dictionary来代替一切集合呢?
在这里我们除了刚才的遍历问题,还要提到Dictionary的存储空间问题,在Dictionary中,除了要存储我们实际需要的Value外,还需要一个辅助变量Key,这就造成了内存空间的双重浪费。
而且在尾部插入时,List只需要在其原有的地址基础上向后延续存储即可,而Dictionary却需要经过复杂的Hash计算,这也是性能损耗的地方。
Comparison and analysis of Dictionary,hashtable,list in C #