Hashtable与Dictionary比较
项目需要存储Tcp连接对象,考虑使用Hashtable或者Dictionary存储。Hashtable在查询方面有优势,Dictionary在确定类型下不需要拆箱与装箱有优势。于是,写了个demo对两个存储对象进行了插入、查询、删除、遍历的速度比较。
static Hashtable hashtable = new Hashtable();
static Dictionary<string, ConnectedClient> keyValuePairs = new Dictionary<string, ConnectedClient>(); static void Init()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for(int i = 0; i < 100000; i++)
{
ConnectedClient connectedClient = new ConnectedClient(new TcpClient());
hashtable.Add(i.ToString(),connectedClient);
}
stopwatch.Stop();
Console.WriteLine(string.Format("Hashtable插入100000耗时{0}。", stopwatch.ElapsedTicks));
stopwatch.Restart();
for (int i = 0; i < 100000; i++)
{
ConnectedClient connectedClient = new ConnectedClient(new TcpClient());
keyValuePairs.Add(i.ToString(), connectedClient);
}
stopwatch.Stop();
Console.WriteLine(string.Format("Dictionary插入100000耗时{0}。", stopwatch.ElapsedTicks)); stopwatch.Restart();
var result = (ConnectedClient)hashtable["70000"];
stopwatch.Stop();
Console.WriteLine(string.Format("Hashtable搜索一条耗时{0}。", stopwatch.ElapsedTicks)); stopwatch.Restart();
result = keyValuePairs["70000"];
stopwatch.Stop();
Console.WriteLine(string.Format("Dictionary搜索一条耗时{0}。", stopwatch.ElapsedTicks)); stopwatch.Restart();
hashtable.Add("9000000", new ConnectedClient(new TcpClient())); ;
stopwatch.Stop();
Console.WriteLine(string.Format("Hashtable插入一条耗时{0}。", stopwatch.ElapsedTicks)); stopwatch.Restart();
keyValuePairs.Add("9000000", new ConnectedClient(new TcpClient())); ;
stopwatch.Stop();
Console.WriteLine(string.Format("Dictionary插入一条耗时{0}。", stopwatch.ElapsedTicks)); stopwatch.Restart();
hashtable.Remove("9000000");;
stopwatch.Stop();
Console.WriteLine(string.Format("Hashtable删除一条耗时{0}。", stopwatch.ElapsedTicks)); stopwatch.Restart();
keyValuePairs.Remove("9000000");;
stopwatch.Stop();
Console.WriteLine(string.Format("Dictionary删除一条耗时{0}。", stopwatch.ElapsedTicks)); stopwatch.Restart();
foreach (var key in hashtable.Keys)
{
((ConnectedClient)hashtable[key]).outCount--;
}
Console.WriteLine(string.Format("Hashtable遍历耗时{0}。", stopwatch.ElapsedTicks));
stopwatch.Stop(); stopwatch.Restart();
foreach (var key in keyValuePairs.Keys)
{
keyValuePairs[key].outCount--;
}
Console.WriteLine(string.Format("Dictionary遍历耗时{0}。", stopwatch.ElapsedTicks));
stopwatch.Stop(); Console.ReadLine();
}

Hashtable与Dictionary比较的更多相关文章
- (转)C#中键值对类型Hashtable与Dictionary比较和相关用法
最近在使用C#中的Hashtable与Dictionary的时候,想知道其区别,通过查找网络相关博客资料,作出下列总结. Hashtable与Dictionary虽然都是作为键值对的载体,但是采用的是 ...
- C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类.我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和 ...
- C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
C#中HashTable.Dictionary.ConcurrentDictionar三者都表示键/值对的集合,但是到底有什么区别,下面详细介绍 一.HashTable HashTable表示键/值对 ...
- C#:Hashtable和Dictionary
Dictionary<TKey, TValue> () Hashtable() 第一.存储的数据类型 Hashtable不是泛型的,不是类型安全的:Dictionary是泛型的, ...
- Hashtable、Dictionary和List 谁效率更高
一 前言 很少接触HashTable晚上回来简单看了看,然后做一些增加和移除的操作,就想和List 与 Dictionary比较下存数据与取数据的差距,然后便有了如下的一此测试, 当然我测的方法可能不 ...
- C#中Hashtable、Dictionary详解以及写入和读取对比
转载:http://www.cnblogs.com/chengxingliang/archive/2013/04/15/3020428.html 在本文中将从基础角度讲解HashTable.Dicti ...
- 深入解析Hashtable、Dictionary、SortedDictionary、SortedList
我们先看Hashtable. MSDN的解释:表示键/值对的集合,这些键/值对根据键的哈希代码进行组织. Hash算法是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定 ...
- C# Hashtable vs Dictionary 学习笔记
Hashtable 和 Dictionary 存储的都是键值对,我的理解是Dictionary是Hashtable的泛型实现. Hashtable的键和值都是object类型.所以,key和value ...
- C#中的HashSet, HashTable, Dictionary的区别【转】
HashSet和Python中的Set差不多,都是为逻辑运算准备的,HashSet不允许数据有重复,且存入的时单值不是键值对. HashTable和Dictionary差不多,但是他们的实现方式时不同 ...
随机推荐
- 如何查看和清除svn登录信息
切换svn登录用 点击clear,出现用户信息,勾选后点击ok 即清除 .下次访问svn需要重新登录
- 国内安装helm
helm repo remove stable helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts ...
- pyautogui 文档(一):简介
PyAutoGUI 可实现控制鼠标.键盘.消息框.截图.定位等功能,最近做了个自动化需要这些,故了解并记录下 自动化需要操作win7上的一个app,用PyAutoGUI做的,定位坐标,点击鼠标等,但是 ...
- 牛客练习赛44C
链接:https://ac.nowcoder.com/acm/contest/634/C来源:牛客网 题目描述 给出一个区间[L,R],求出[L,R]中孪生质数有多少对. 由于这是一个区间筛质数的模板 ...
- Linux操作系统df相关问题解惑
1.df 命令无法使用解决办法 问题分析: 1.1 df命令是通过/etc/mtab文件读取已挂载文件系统的信息,因此,df命令无法使用的原因的在于/etc/mtab文件被损坏或者丢失. 查看 mor ...
- Laravel 5 速查表
Artisan // 在版本 5.1.11 新添加,见 http://d.laravel-china.org/docs/5.1/authorization#creating-policiesphp a ...
- redis单例模式
看到好多面试都问设计模式,我就简单的了解了一下,顺便把之前封装好的Reis做了一次修改. 单例模式(Singleton Pattern 单件模式或单元素模式) 单例模式确保某个类只有一个实例,而且自行 ...
- MR汇聚工具步骤
---------------------------------MR汇聚工具步骤------------------------------------- 1.需要连上141服务器 用户:root ...
- navicat使用ssh登录mysql报错:expected key exchange group packet from server
转载自:https://blog.csdn.net/enweitech/article/details/80677374 解决方法: vim /etc/ssh/sshd_config shift+g ...
- windows 环境下 dbnamodb 环境搭建与使用
https://docs.aws.amazon.com/zh_cn/cli/latest/userguide/installing.html 安装 AWS Command Line Interface ...