C#中Dictionary,Hashtable,List的比较及分析
一. Dictionary与Hashtable
Dictionary与Hashtable都是.Net Framework中的字典类,能够根据键快速查找值
二者的特性大体上是相同的,有时可以把Dictionary<K,V>看做是Hashtable的泛型版本。不过Hashtable是线程安全的,Dictionary是有序的。
字典的性能取决于键类型的GetHashCode()方法的实现代码。
键类型也必须实现IEquatable<T>.Equals()方法,并且如果A.Equals(B)返回true,则A和B的GetHashCode()也必须返回相同的值。
Dictionary
- 有泛型优势(类型安全,性能更好),对于值类型,不存在装箱和拆箱的性能损耗
- 读取速度快(体现在单条数据上)
- 容量利用更充分
- 有序(遍历时输出的顺序就是加入的顺序)
Hashtable
- 适合多线程
- 通过静态方法Synchronize方法可获得完全线程安全的类型
- 无序
二.List与Dictionary
List有点类似于Dictionary。二者都具有使用泛型的优点,Dictionary没有在内存中移动后续元素的性能开销。
List是在数组的基础上做的封装,遍历查询更快(数据较多时),Dictionary单条查询更快
引用某一篇文章的分析:
同样是集合,为什么性能会有这样的差距。我们要从存储结构和操作系统的原理谈起。首先我们清楚List<T>是对数组做了一层包装,我们在数据结构上称之为线性表,而线性表的概念是,在内存中的连续区域,除了首节点和尾节点外,每个节点都有着其唯一的前驱结点和后续节点。我们在这里关注的是连续这个概念。而HashTable或者Dictionary,他是根据Key和Hash算法分析产生的内存地址,因此在宏观上是不连续的,虽然微软对其算法也进行了很大的优化。由于这样的不连续,在遍历时,Dictionary必然会产生大量的内存换页操作,而List只需要进行最少的内存换页即可,这就是List和Dictionary在遍历时效率差异的根本原因。6. 再谈Dictionary也许很多人说,既然Dictionary如此强大,那么我们为什么不用Dictionary来代替一切集合呢?在这里我们除了刚才的遍历问题,还要提到Dictionary的存储空间问题,在Dictionary中,除了要存储我们实际需要的Value外,还需要一个辅助变量Key,这就造成了内存空间的双重浪费。而且在尾部插入时,List只需要在其原有的地址基础上向后延续存储即可,而Dictionary却需要经过复杂的Hash计算,这也是性能损耗的地方。
using System.Collections;
using System.Collections.Generic; namespace ConsoleApp
{
class Program
{
public const int TOTAL_COUNT = ; public static void Main(string[] args)
{
ListTest();
DicTest();
HashtableTest();
ListInsertTest();
} private static void HashtableTest()
{
Hashtable ht = new Hashtable();
for (int i = ; i < TOTAL_COUNT; i++)
{
ht.Add(i, new Model { Num = i });
}
} public static void ListTest()
{
List<Model> list = new List<Model>();
for (int i = ; i < TOTAL_COUNT; i++)
{
list.Add(new Model { Num = i });
}
}
public static void ListInsertTest()
{
List<Model> list = new List<Model>();
list.Insert(, new Model { Num = });
list.Insert(, new Model { Num = });
for (int i = ; i < TOTAL_COUNT; i++)
{
list.Insert(,new Model { Num = i });
}
} public static void DicTest()
{
Dictionary<int, Model> dic = new Dictionary<int, Model>();
for (int i = ; i < TOTAL_COUNT; i++)
{
dic.Add(i, new Model { Num = i });
}
} public class Model
{
public int Num { set; get; }
}
}
}
测试代码

using System.Collections;
using System.Collections.Generic;
using System.Linq; namespace ConsoleApp
{
class Program
{
public const int TOTAL_COUNT = ; public static void Main(string[] args)
{
List<Model> list = new List<Model>();
for (int i = ; i < TOTAL_COUNT; i++)
{
list.Add(new Model { Num = i });
} Hashtable ht = new Hashtable();
for (int i = ; i < TOTAL_COUNT; i++)
{
ht.Add(i,new Model { Num = i });
} Dictionary<int, Model> dic = list.ToDictionary(l=>l.Num); ListTest(list);
DicTest(dic);
HashtableTest(ht);
} private static void HashtableTest(Hashtable ht)
{
foreach (var key in ht.Keys)
{
var rst = ht[key];
}
} public static void ListTest(List<Model> list)
{
foreach (var item in list)
{
var rst = item;
}
} public static void DicTest(Dictionary<int, Model> dic)
{
foreach (var key in dic.Keys)
{
var rst = dic[key];
}
} public class Model
{
public int Num { set; get; }
}
}
}
遍历测试代码
还是100W条数据

C#中Dictionary,Hashtable,List的比较及分析的更多相关文章
- C#中Dictionary、ArrayList、Hashtable和Array的区别
IDictionary接口是所有字典类集合的基本接口,该接口与ICollection,IEnumerable接口是所有非泛型类集合的最基本的接口 IEnumerable接口用于公开枚举数,该枚举数支持 ...
- .NET中Dictionary<TKey, TValue>浅析
.NET中Dictionary<TKey, Tvalue>是非常常用的key-value的数据结构,也就是其实就是传说中的哈希表..NET中还有一个叫做Hashtable的类型,两个类型都 ...
- 【C# 集合】HashTable .net core 中的Hashtable的实现原理
上一篇我介绍了Hash函数 这篇我来说一下Hash函数在 HashTable中的应用. HashTable的特性: 1.装载因子:.net core 0.72 ,java 0.75 2.冲突解决方案: ...
- C#中Dictionary<TKey,TValue>排序方式
自定义类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...
- C#中Dictionary小记
使用C#中Dictionary的一下细节小记: 一.Dictionary.Add(key,value) 与 Dictionary[key]=value的区别: 如果Dictionary中已经有了key ...
- 在webservice中传递Hashtable
webservice中不支持hashtable的数据类型,那么如何在webservice中传递hashtable呢?我们可以通过将hashtable转化为webservice中支持的数组的类型来进行传 ...
- C#中Dictionary的用法
在C#中,Dictionary提供快速的基于兼职的元素查找.他的结构是这样的:Dictionary<[key], [value]> ,当你有很多元素的时候可以使用它.它包含在System. ...
- ASP.NET中Dictionary基本用法实例分析
本文实例讲述了ASP.NET中Dictionary基本用法.分享给大家供大家参考,具体如下: //Dictionary位于System.Collections.Generic命名空间之下 /* * ...
- C#中Dictionary排序方式
转载自:https://www.cnblogs.com/5696-an/p/5625142.html 自定义类: https://files.cnblogs.com/files/xunhanliu/d ...
随机推荐
- BABOK2主要概要输入输出图
- ASP.NET MVC 在控制器中接收视图表单POST过来的数据方法
方法一:通过Request.Form [HttpPost] public ActionResult Test() { string id=Reques ...
- String-------RegularHelper
/// <summary> /// 正则表达式相关方法集合 /// </summary> public static class RegularHelper { private ...
- IOS开发UI基础文本属性Attributes
文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFo ...
- QCustomplot使用分享(一) 能做什么事
一.QCustomPlot简介 之前在Qt之自绘制饼图这篇文章的说明中我简单的描述了下目前依赖于qt的第三方绘图库,此后我会针对自己使用QCustomPlot的情况做一总结,以方便大家参考 QCust ...
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
- App.Config详解
App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...
- 用Perl编写Apache模块
前言 Apache被许多大流量网站所嫌弃,但很多企业级的场景则更为适用. Apache httpd 从 2.0 之后,已经不仅仅局限于一个 http 的服务器,更是一个完善而强大.灵活而健壮且容易扩展 ...
- Xcode配置libdc1394
libdc1394是一个开源库,提供了一个Mac下完整的1394相机编程接口,这篇文章将介绍Xcode如何配置该库. 步骤: 1.下载libdc1394的源码,并解压 http://damien.do ...
- oracle user account locked
1.Question describe when you use account scott/tiger connect to oracle, you will see "the user ...