项目需要存储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. 第六周博客作业 <西北师范大学| 周安伟>

    第六周博客作业 助教博客链接:https://home.cnblogs.com/u/zaw-315/ 本周工作:评阅作业24份点评困难的作业:无作业要求:https://www.cnblogs.com ...

  2. python环境搭建(linux)

    python安装 # wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz # yum install openssl-devel ...

  3. 企业BGP网络规划案例(三)

    路由选路 1.分支的办公流和生产流默认走联通MSTP线路,DC流默认走电信MSTP线路,当其中某条互联的链路down后才会进行流量切换 XRV1配置 ========================= ...

  4. RFID概述

    自动识别技术的本质在于利用被识别物理对象的一些具有辨识度的特征来对物理对象进行区分和识别.因此,这些具有辨识度的特征可以是物理对象自带的特征,如指纹,人脸,语言,视网膜,心跳等,也可以是通过第三方赋予 ...

  5. vue使用qrcode插件生成二维码

    参考:https://www.jianshu.com/p/d3883e020d99 步骤: 第一步:vue-cli下载插件 cnpm install --save qrcodejs2 第二步:组件中引 ...

  6. Hillstone基础上网配置

    1.接口配置 1)进入设备管理界面后,点击左边 网络—接口: 2)选择相应的接口,点击编辑: 3)在接口配置界面中,选择接口的安全域类型(三层接口即为三层安全域,二层接口即为二层安全域).安全域名称 ...

  7. 构造一个String类

    #include "stdafx.h" #include<iostream> #include<string.h> using namespace std; ...

  8. php JS 导出表格特殊处理

    但是这样导出身份证号会变为科学计数: 解决方法就是: 我们了解一下excel从web页面上导出的原理.当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取 它,所以把mi ...

  9. 云计算底层技术-虚拟网络设备(Bridge,VLAN)( 转发)

    云计算底层技术-虚拟网络设备(Bridge,VLAN) Posted on September 24, 2017 by opengers in openstack openstack底层技术-各种虚拟 ...

  10. [DP][NOIP2015]子串

    子串 题目描述 有两个仅包含小写英文字母的字符串 A 和 B. 现在要从字符串 A 中取出 k 个 互不重叠 的非空子串, 然后把这 k 个子串按照其在字符串 A 中出现的顺序依次连接起来得到一个新的 ...