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 ...
随机推荐
- static、final、static final 用法
1.使用范围:类.方法.变量.2.区别和联系:2.1.static 含义:静态的,被 static 修饰的方法和属性只属于类不属于类的任何对象.2.2.static 用法:2.2.1.static 可 ...
- mysql 在row模式下truncate 与 delete 二进制日志记录的差异
二进行日志的格式为row mysql> show variables like 'binlog_format'; +---------------+-------+ | Variable_nam ...
- WdatePicker.js的使用方法
WdatePicker.js的使用方法 摘自:http://www.cnblogs.com/wuchao/archive/2012/07/19/2599209.html 4. 日期范围限制 静态限制 ...
- ASP.NET十分有用的页面间传值方法
一.目前在ASP.NET中页面传值共有这么几种方式: 1.表单提交, <form action= "target.aspx" method = "post&qu ...
- WebApi传参总动员(四)
前文介绍了Form Data 形式传参,本文介绍json传参. WebApi及Model: public class ValuesController : ApiController { [HttpP ...
- Fluent Nhibernate and Stored Procedures
sql:存储过程 DROP TABLE Department GO CREATE TABLE Department ( Id INT IDENTITY(1,1) PRIMARY KEY, DepNam ...
- jquery.cookie.js 用法
jquery.cookie.js 用法 一个轻量级的cookie 插件,可以读取.写入.删除 cookie. jquery.cookie.js 的配置 首先包含jQuery的库文件,在后面包含 j ...
- mysql创建数据库指定编码
GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; UTF8: CREATE DATABASE ` ...
- C#6.0语法糖剖析(一)
1.自动属性默认初始化 使用代码 "; 编译器生成的代码: public class Customer { [CompilerGenerated] private string kBacki ...
- Socket 学习实例
整理一份Socket代码,整理前辈的代码 http://www.cnblogs.com/yellowapplemylove/archive/2011/04/19/2021586.html 直接贴代码 ...