近期做版本迭代任务,有一个在店铺头部展示店主所在的城市名称和省份名称的需求,店主信息表中保存了店主所在的城市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. \extras\intel\Hardware_Accelerated_Execution_Manager HAXM 未安装导致AndroidStudio新建了模拟器开启不了

    之前安装过 bios模式也是正常的.所以按照下面的步骤操作. https://software.intel.com/en-us/android  在这个界面 选择右侧的, 下载后解压到下面图上的路径: ...

  2. HttpClient 超时时间

    setSoTimeout(MilSec):连接超时时间.如果在连接过程中有数据传输,超时时间重新计算. setConnectTimeout(MilSec):获取连接超时时间.如果该参数没有设置,那么默 ...

  3. 使用JS伪造Post请求

    [使用JS伪造Post请求] 提到伪造Post请求,首先想到的是构造HTTP包.但实际上有一种更简单的方法,构造HTML FORM表单,使用js进行提交.如下:

  4. cloudrea manager 调整datanode数据存储目录

    由于datanode所需磁盘空间较大,所以工作中可能会涉及到给datanode增加磁盘目录或者更改数据目录 CM停止该datanode节点 CM页面增加目录或者修改目录 如果是修改目录的话 需要将服务 ...

  5. jdbcTemplate in

    参考 http://blog.csdn.net/gaopeng0071/article/details/75049952 使用NamedParameterJdbcTemplate public cla ...

  6. oracle,PL/SQL新建表

    创建用户 -- Create the user create user U_HQ_JAVA default tablespace USERS temporary tablespace TEMP pro ...

  7. cdoj841-休生伤杜景死惊开 (逆序数变形)【线段树 树状数组】

    http://acm.uestc.edu.cn/#/problem/show/841 休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others)     Memory ...

  8. 122. Best Time to Buy and Sell Stock II (Array;Greedy)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. collections系列之OrderedDict【有序字典】与DefaultDict【默认字典】

    今天来向大家介绍一下collections系列中的OrderedDict和DefaultDict,这两种类均是通过collections来创建的,均是对dict字典加工,所有都继承了dict字典的方法 ...

  10. curl: (60) SSL certificate problem: unable to get local issuer certificate 错误

    今天同事做微信管理的项目,请求接口返回如下错误SSL certificate problem: unable to get local issuer certificate. 此问题的出现是由于没有配 ...