近期做版本迭代任务,有一个在店铺头部展示店主所在的城市名称和省份名称的需求,店主信息表中保存了店主所在的城市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. vmware搭建vSAN提示磁盘不合格或者看不到磁盘的解决办法

    1.如果磁盘不合格,或者在创建磁盘组的时候看不到该磁盘,一般的原因都是该磁盘有其他分区,可以使用下面的方法来解决 a.首先开启esxi主机的ssh功能 b.登陆到esxi的后台 c. ls /dev/ ...

  2. 基于快速排序的数组划分:2组 3组 K组(sort color)大小写排序 · Partition Array

    2组: [抄题]: 给出一个整数数组 nums 和一个整数 k.划分数组(即移动数组 nums 中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中 ...

  3. Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this scrip

    在运行phpize时出现的错误 > /data/php/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api ...

  4. net下载文件方法汇总

    //TransmitFile实现下载 protected void Button1_Click1(object sender, EventArgs e) { /* 微软为Response对象提供了一个 ...

  5. vue2.0 tab切换几种方式

    第一种 比较灵活简单的方式(切换改变部分的内容在组件中比较方便操作) <template> <div id="app"> <ul> <li ...

  6. dataTables常用参数

    一.新版本和老版本的区别 新版本的改进:https://datatables.net/new/1.10 新老版本参数变化列表:http://datatables.club/upgrade/1.10-c ...

  7. Spring 学习记录3 ConversionService

    ConversionService与Environment的关系 通过之前的学习(Spring 学习记录2 Environment),我已经Environment主要是负责解析properties和p ...

  8. 二进制搭建kubernetes多master集群【开篇、集群环境和功能介绍】

    本文主要说明kubernetes集群使用组建的版本和功能介绍.. 一.组件版本 Kubernetes 1.12.3 Docker 18.06.1-ce Etcd 3.3.10 Flanneld 0.1 ...

  9. 处理No CPU/ABI system image for target的方法

    处理No CPU/ABI system image for target的方法 最近菩提搭建完成Android开发环境后,在创建安卓模拟器的时候遇到了问题.这个问题就是图片中显示的no CPU/ABI ...

  10. composer 安装扩展失败的决绝方法

    https://getyii.com/topic/32