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差不多,但是他们的实现方式时不同 ...
随机推荐
- DDB---查询与优化
摘要:分布式数据库(Distributed DB)是数据库中非常重要的一个部分,随着要处理的数据越来越多,分布式逐渐成为了一种策略.主要有:分布式操作系统,分布式程序设计语言,分布式文件系统,分布式数 ...
- 如何把ppt写好
前言 这里不是总结ppt如何写的美观漂亮,而是总结写整体的结构,如何修辞 整体结构 前后呼应 就像做作文一样,前后呼应,产生共鸣和联想,切记过于离散 抽象化 不要把自己做的东西写小了,要有一定的抽象度 ...
- 微信小程序开发过程问题总汇
之前在开发一个控车小程序,把过程中稍微需要搜索的问题做了记录. 1. 关键词:本地资源图片无法通过WXSS获取 描述:做小程序开发的时候,如果你需要使用图片作为背景,也就是想使用background- ...
- django 富文本编辑器
创建工程,数据中数据格式设置为 models.TextField() 此时编辑器是普通的文本框. 使用 tinymce .安装 django-tinymce pip install django-ti ...
- 1、背景介绍及移动云MAS平台 --短信平台
目的: 刚开发完成一套短信平台以及一个Web端短信发送系统,短信平台耗时两个周.短信发送系统耗时两个多月,开发使用的技术没什么高科技含量,在此主要是记录下很多情况的处理方案,希望能让大家提出改善方案和 ...
- Spring Cloud的概述(二)
1.什么是spring cloud? spring cloud,基于spring boot提供了一套微服务的解决方案,包括服务的注册与发现,配置中心,全链路监控,服务网管,负载均衡,熔断等组件,除了基 ...
- Flip String to Monotone Increasing LT926
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...
- web网站使用qq第三方登录
Html代码: <a href=’/QQlogin’>qq登录</a> //后台代码: @RequestMapping(value = "/QQlogin" ...
- mysql 跨服务器查询
转载地址:https://blog.csdn.net/pashine/article/details/78875540
- 字典 Dictionary
字典 Dictionary {Key-Value} 1.字典是无序的,没有下标(因为有key,取值直接用key值) Key尽量不要用中文编写,以防止编码不同导致取不出来 2.字典常用方法: 查找: ① ...