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 ...
随机推荐
- 资料下载:敏捷个人的成长思考.pptx(第1次线下活动2011.04)
本文挪至 http://www.zhoujingen.cn/blog/629.html PDF下载地址:http://down.51cto.com/data/207112 推荐:你可能需要的在线电子书 ...
- Hibernate核心类用法-使用Transaction管理事务
一个典型的事务应该使用下面的形式 在创建完Session对象后即使用beginTransaction()启动事务 从此开始直到commit()之间的代码 都会处于同一个事务中 这两个函数之间所有的数据 ...
- js-基础(1)
js-基础(1) javascript由三部分组成:核心,DOM,BOM核心——ECMAScript,可以运行浏览器/单纯的JS引擎 console.log(‘hello’);DOM——操作HT ...
- [阅读]个人阅读作业week7
People-oriented in Agile People-oriented in Agile One Leader Prepare Good ideas from users People-or ...
- 数论 - 算数基本定理的运用 --- nefu 118 : n!后面有多少个0
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemshow.php Mean: 略. analyse: 刚开始想了半天都没想出来,数据这么大,难道是有什么 ...
- error LNK1281: 无法生成 SAFESEH 映像 LNK2026 模块对于 SAFESEH 映像是不安全的 VS2015 /win10
平台 VS2015 /win10 错误 LNK1281 无法生成 SAFESEH 映像. 错误 LNK2026 模块对于 SAFESEH 映像是不安全的. 将 release 改成 debug
- js禁止复制页面文字
做项目的时候有客户提出要求,不能用用户浏览他发表的文章时复制他的文章 一种比较简单的方法,禁止用户选中页面的文字和禁止用户右键菜单 document.oncontextmenu = new Funct ...
- 与众不同 windows phone (41) - 8.0 相机和照片: 通过 AudioVideoCaptureDevice 捕获视频和音频
[源码下载] 与众不同 windows phone (41) - 8.0 相机和照片: 通过 AudioVideoCaptureDevice 捕获视频和音频 作者:webabcd 介绍与众不同 win ...
- ati显卡驱动后,性能不咋地
ubuntu装了ati的闭源驱动后,拖动窗口,会有明显的断裂感,不够平滑
- Gson解析的小例子
最近解析些复杂的节点数据解析,用安卓自带的json解析比较麻烦所以只能用Gson解析,所以从网上下了点demo来看看 http://blog.csdn.net/tkwxty/article/detai ...