近期做版本迭代任务,有一个在店铺头部展示店主所在的城市名称和省份名称的需求,店主信息表中保存了店主所在的城市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. linux - 文件拆分

    核心: split 例如,把一个文件以10万行为单位拆分文件, 并且以perfix作为前缀,3位数数字,从000开始递增 split -l 100000 filename.txt -d -a 3 pe ...

  2. JAVA序列化和反序列化 对象<=>IO流 对象<=>字节数组

    http://developer.51cto.com/art/201202/317181.htm http://blog.csdn.net/earbao/article/details/4691440 ...

  3. asp.net回发页面被刷新后悔重新执行回发事件的解决方法

    做项目,进行数据修改操作后,重新加载数据,本来是没问题的.但是在这个时候刷新下页面,发现修改操作又重新执行了一次,并弹出“修改成功”的提示框. 百度了下,找到以下解决方法,解决了问题: Page.Cl ...

  4. android:cmd下面用adb打log

    进入cmd命令行,启动adb 1.用adb打log:adb logcat 2.过滤log信息:adb logcat | findstr ***   这里的***就是你需要设置的过滤项,如myscan ...

  5. xcode - 显示安装过的低版本模拟器

    1. 更改版本

  6. web 批量打印

    批量打印,同时打印多个页面,有两种思路: 第一种思路,将所有的页面内容加载到一个页面中,然后再打印.这种打印方式有几个弊端,页面的样式会丢失,页面太多同时加载到一个页面中,数据量太大,响应时间很长,消 ...

  7. loadrunner--常用函数列表【转】

    1.        Intweb_reg_save_param("参数名","LB=左边界","RB=右边界",LAST);/注册函数,在参 ...

  8. dump()

    输出格式化的对象

  9. 【Linux】目录文件权限的查看和修改【转】

    转载自:http://zhaoyuqiang.blog.51cto.com/6328846/1214718 ============================================== ...

  10. Devexpress VCL Build v2014 vol 14.2.7发布

    2015年马上快过去一半了,这个玩意还在纠结在14版.其实也无所谓,反正就是改成15版,也还是这些 东西的修补. What's New in 14.2.7 (VCL Product Line)   N ...