项目需要存储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比较的更多相关文章

  1. (转)C#中键值对类型Hashtable与Dictionary比较和相关用法

    最近在使用C#中的Hashtable与Dictionary的时候,想知道其区别,通过查找网络相关博客资料,作出下列总结. Hashtable与Dictionary虽然都是作为键值对的载体,但是采用的是 ...

  2. C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)

    我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类.我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和 ...

  3. C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别

    C#中HashTable.Dictionary.ConcurrentDictionar三者都表示键/值对的集合,但是到底有什么区别,下面详细介绍 一.HashTable HashTable表示键/值对 ...

  4. C#:Hashtable和Dictionary

    Dictionary<TKey, TValue> ()      Hashtable() 第一.存储的数据类型 Hashtable不是泛型的,不是类型安全的:Dictionary是泛型的, ...

  5. Hashtable、Dictionary和List 谁效率更高

    一 前言 很少接触HashTable晚上回来简单看了看,然后做一些增加和移除的操作,就想和List 与 Dictionary比较下存数据与取数据的差距,然后便有了如下的一此测试, 当然我测的方法可能不 ...

  6. C#中Hashtable、Dictionary详解以及写入和读取对比

    转载:http://www.cnblogs.com/chengxingliang/archive/2013/04/15/3020428.html 在本文中将从基础角度讲解HashTable.Dicti ...

  7. 深入解析Hashtable、Dictionary、SortedDictionary、SortedList

    我们先看Hashtable. MSDN的解释:表示键/值对的集合,这些键/值对根据键的哈希代码进行组织. Hash算法是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定 ...

  8. C# Hashtable vs Dictionary 学习笔记

    Hashtable 和 Dictionary 存储的都是键值对,我的理解是Dictionary是Hashtable的泛型实现. Hashtable的键和值都是object类型.所以,key和value ...

  9. C#中的HashSet, HashTable, Dictionary的区别【转】

    HashSet和Python中的Set差不多,都是为逻辑运算准备的,HashSet不允许数据有重复,且存入的时单值不是键值对. HashTable和Dictionary差不多,但是他们的实现方式时不同 ...

随机推荐

  1. windows server 2012R2 故障转移集群配置

    配置说明: AD:10.10.1.10/24  Node-2:10.10.1.20/24 Node-3:10.10.1.30/24 zhangsan-PC:10.10.1.50/24  VIP1:10 ...

  2. layer弹出层不居中解决方案(转)

    @感谢参考文章 原文内容: 一.问题描述 用layer做操作结果提示时,发现如果页面超出屏幕的高度时,弹出的提示不是屏幕居中,而是在页面高度的中间,如果一个页面的高度比较大,就看不到提示了. 还有一种 ...

  3. 思科模拟器GNS3-2.1.8安装笔记 (适用于版本2.0.3以上的GNS3)

    当前现阶段学习经常使用的路由交换设备主要来自于思科.华为和华三三家,这三家的设备操作配置大致类似,却又不尽相同.因为实体设备通常都非常昂贵,所以作为学习,我们通常会使用它们提供的模拟器.华为的模拟器是 ...

  4. 4-18 class与id的区别

    1. class是设置标签的类, class属性用于指定元素属于何种样式的类. 如样式表可以加入.content1 { color: red; background: #ff80c0 } 使用方法:c ...

  5. mysql7.5.x删除重新安装

    删除: cmd管理员运行,进入D:\devs\MySQL\mysql-5.7.25-winx64\bin目录下: 输入命令:sc delete mysql 删除data目录下的所有文件 安装: 创建m ...

  6. Linux驱动之USB鼠标驱动编写

    本篇博客分以下几部分讲解 1.介绍USB四大描述 2.介绍USB鼠标驱动程序功能及框架 3.介绍程序用到的结构体 4.介绍程序用到的函数 5.编写程序 6.测试程序 1.介绍USB四大描述符 USB设 ...

  7. Java-HashMap、HashSet、hashTable

    HashMap:key:可null,重复的key对应的value会出现后面覆盖前面的情况.value:可null; 引用:数组,数组中存链表. HashSet使用HashMap的实现存储数据,所以有H ...

  8. ABP 依赖注入

    1.ABP自动注入 //IapplicationService注入方式暴露接 //ITransientDependency 不会暴露接口 ITransientDependency和ISingleton ...

  9. apache ab 结果Failed requests探究

    Failed requests: 537 (Connect: 0, Receive: 3, Length: 268, Exceptions: 266) Receive:当客户端connect成功后,并 ...

  10. SOP - Validation

    Table of Contents目录表1 Roles and Responsibilities related to validation与验证相关的1个角色和职责2 Introduction2引言 ...