近期做版本迭代任务,有一个在店铺头部展示店主所在的城市名称和省份名称的需求,店主信息表中保存了店主所在的城市Id和省份Id,由于原有业务复杂,要尽量减少Sql执行时间,所以不考虑join城市地区详细表。于是考虑在集合类中处理。

是选择Hashtable还是Dictionary<T,T>呢?于是做了一个测试,代码清单如下:

 /// <summary>
/// 城市信息
/// </summary>
public class CityInfo
{
public string CityName { get; set; }
public string ProvinceName { get; set; }
}
/// <summary>
/// 数据仓库和查找方法
/// </summary>
public static class Respository
{ public static Hashtable Retrieve()
{
Hashtable data = new Hashtable();
for (int i = ; i < ; i++)
{
data.Add(i+,"data"+i);
}
return data;
}
public static string Find(Hashtable data,int id)
{
var query = from item in data.Cast<DictionaryEntry>() where (int)item.Key == id select item.Value.ToString();
return query.First();
} public static Dictionary<int, string> SecondRetrieve() {
Dictionary<int,string> data = new Dictionary<int,string>();
for (int i = ; i < ; i++)
{
data.Add(i + , "data" + i);
}
return data;
} public static string SecondFind(Dictionary<int, string> data, int id)
{
var query = from item in data where (int)item.Key == id select item.Value;
return query.First();
} public static Dictionary<int, CityInfo> ThridRetrieve()
{
Dictionary<int, CityInfo> data = new Dictionary<int, CityInfo>();
for (int i = ; i < ; i++)
{
data.Add(i + , new CityInfo { CityName="CityName"+i, ProvinceName="ProvinceName"+i });
}
return data;
} public static CityInfo ThridFind(Dictionary<int, CityInfo> data, int id)
{
var query = from item in data where (int)item.Key == id select item.Value;
return query.First();
}
}

测试入口:

 static void Main(string[] args)
{
{
Stopwatch stwtotal = new Stopwatch();
stwtotal.Start();
Hashtable data = Respository.Retrieve();
for (int i = ; i < ; i++)
{
Stopwatch stw = new Stopwatch();
stw.Start();
Random r = new Random();
string result = Respository.Find(data, r.Next(, )*i);
stw.Stop();
Console.WriteLine(result + " FirstFindExecuteTime:" + stw.Elapsed.TotalMilliseconds+"ms");
}
stwtotal.Stop();
Console.WriteLine("测试数据总时间:" + stwtotal.Elapsed.TotalMilliseconds + "ms");
}
{
Stopwatch stwtotal = new Stopwatch();
stwtotal.Start();
Dictionary<int, string> data = Respository.SecondRetrieve();
for (int i = ; i < ; i++)
{
Stopwatch stw = new Stopwatch();
stw.Start();
Random r = new Random();
string result = Respository.SecondFind(data, r.Next(, )*i);
stw.Stop();
Console.WriteLine(result + " SecondFindExecuteTime:" + stw.Elapsed.TotalMilliseconds+"ms");
}
stwtotal.Stop();
Console.WriteLine("测试数据总时间:" + stwtotal.Elapsed.TotalMilliseconds + "ms");
}
{
Stopwatch stwtotal = new Stopwatch();
stwtotal.Start();
Dictionary<int, CityInfo> data = Respository.ThridRetrieve();
for (int i = ; i < ; i++)
{
Stopwatch stw = new Stopwatch();
stw.Start();
Random r = new Random();
CityInfo result = Respository.ThridFind(data, r.Next(, ) * i);
stw.Stop();
Console.WriteLine(result.CityName + " ThridFindExecuteTime:" + stw.Elapsed.TotalMilliseconds + "ms");
}
stwtotal.Stop();
Console.WriteLine("真实数据初始化到查找结束总时间:" + stwtotal.Elapsed.TotalMilliseconds + "ms");
}
Console.ReadKey();
}

测试结论:Dictionary<T,T>明显优于Hashtable

截图如下:

1.Hashtable

2Dictionary<int,string>

3,Dictionary<int,CityInfo>

使用Linq对Hashtable和Dictionary<T,T>查询的效率比较的更多相关文章

  1. Linq在Array,List,Dictionary中的应用

    Linq在Array,List,Dictionary中的应用 今天在实际工作中需要对array,list,dictionary进行排序,试一试linq,发现非常好用,代码如下: using Syste ...

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

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

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

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

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

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

  5. C#:Hashtable和Dictionary

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

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

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

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

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

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

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

  9. Hashtable与Dictionary比较

    项目需要存储Tcp连接对象,考虑使用Hashtable或者Dictionary存储.Hashtable在查询方面有优势,Dictionary在确定类型下不需要拆箱与装箱有优势.于是,写了个demo对两 ...

随机推荐

  1. ORDER BY 子句在视 图、内联函数、派生表、子查询和公用表表达式中无效

    SQL语句: select * from (select distinct t2.issue,cashmoney from (select distinct issue from lot_gamepa ...

  2. 慕课网access_token的获取

    access_token的接口是微信公众号一个基础接口,access_token接口微信公众号一个非常重要的接口 access_token是微信公众号的全局唯一票据,公众号的所有接口的调用都需要使用到 ...

  3. oracle在centos6.5安装

    说明 很多操作是默认,具体定制另说. 安装 参考http://www.linuxidc.com/Linux/2014-02/97374p4.htm 这篇是上面那篇的整合版:http://www.cnb ...

  4. python之面向对象之继承

    #写一个类SchoolMember class SchoolMember(object): member_num = 0 def __init__(self,name,age,sex): self.n ...

  5. 关于weblogic报UnsatisfiedLinkError Native Library xxx.so already loaded

    一.场景 最近写的一个系统,在Tomcat测试完后说要改使用weblogic,于是在服务器上安装了weblogic,捣鼓了半天,一个个问题冒了出来,其中就有个比较麻烦的报错:UnsatisfiedLi ...

  6. visio2003 数据表模型中显示字段类型和注释

    1.在visio菜单上选择 数据库->选项->文档. 2.在常规中找到 [在图表中可见的名称] 选中 两者. 3.在表中找到 [数据类型] 选中 显示物理. 4.在数据表模型中创建字段,并 ...

  7. spring开发Eclipse需要做设置

    1. 统一工作空间的编码,选择UTF-8 2. 把创建JSP页面的编码修改UTF-8 3. 重新配置Tomcat服务器 * 先配置Tomcat服务器 * 选择服务器 --> open --> ...

  8. mvn 使用

    简介 本文将介绍基于 Apache Maven 3 的项目构建的基本概念和方法.Maven 是一套标准的项目构建和管理工具,使用统一规范的脚本进行项目构建,简单易用,摒弃了 Ant 中繁琐的构建元素, ...

  9. 一定要 先删除 sc表 中的 某元组 行,,, 再删除 course表中的 元组行

    一定要  先删除 sc表 中的  某元组   行,,, 再删除  course表中的  元组行 course表 SC表 删除  course表中的  元组行,,出现错误 sc    ---->参 ...

  10. centos7 编译安装mysql

    centos 7 安装mySql   1,准备mySql源码安装 #wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.23.tar ...