使用Linq对Hashtable和Dictionary<T,T>查询的效率比较

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

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

 1 /// <summary>
 2     /// 城市信息
 3     /// </summary>
 4     public class CityInfo
 5     {
 6         public string CityName { get; set; }
 7         public string ProvinceName { get; set; }
 8     }
 9     /// <summary>
10     /// 数据仓库和查找方法
11     /// </summary>
12     public static  class Respository
13     {
14 
15         
16         public static Hashtable Retrieve()
17         {
18             Hashtable data = new Hashtable();
19             for (int i = 0; i < 100000; i++)
20             {
21              data.Add(i+1,"data"+i);
22             }
23             return data;
24         }
25         public static string Find(Hashtable data,int id)
26         {
27             var query = from item in data.Cast<DictionaryEntry>() where (int)item.Key == id select item.Value.ToString();
28             return query.First();
29         }
30 
31 
32         public static Dictionary<int, string> SecondRetrieve()
33 
34         {
35             Dictionary<int,string> data = new Dictionary<int,string>();
36             for (int i = 0; i < 100000; i++)
37             {
38                 data.Add(i + 1, "data" + i);
39             }
40             return data;
41         }
42 
43         public static string SecondFind(Dictionary<int, string> data, int id)
44         {
45             var query = from item in data where (int)item.Key == id select item.Value;
46             return query.First();
47         }
48 
49         public static Dictionary<int, CityInfo> ThridRetrieve()
50         {
51             Dictionary<int, CityInfo> data = new Dictionary<int, CityInfo>();
52             for (int i = 0; i < 100000; i++)
53             {
54                 data.Add(i + 1,  new CityInfo { CityName="CityName"+i, ProvinceName="ProvinceName"+i  });
55             }
56             return data;
57         }
58 
59         public static CityInfo ThridFind(Dictionary<int, CityInfo> data, int id)
60         {
61             var query = from item in data where (int)item.Key == id select item.Value;
62             return query.First();
63         }
64     }

测试入口:

 1 static void Main(string[] args)
 2         {
 3             {
 4                 Stopwatch stwtotal = new Stopwatch();
 5                 stwtotal.Start();
 6                 Hashtable data = Respository.Retrieve();
 7                 for (int i = 1; i < 20; i++)
 8                 {
 9                     Stopwatch stw = new Stopwatch();
10                     stw.Start();
11                     Random r = new Random();
12                     string result = Respository.Find(data, r.Next(1, 1000)*i);
13                     stw.Stop();
14                     Console.WriteLine(result + "   FirstFindExecuteTime:" + stw.Elapsed.TotalMilliseconds+"ms");
15                 }
16                 stwtotal.Stop();
17                 Console.WriteLine("测试数据总时间" + stwtotal.Elapsed.TotalMilliseconds + "ms");
18             }
19             {
20                 Stopwatch stwtotal = new Stopwatch();
21                 stwtotal.Start();
22                 Dictionary<int, string> data = Respository.SecondRetrieve();
23                 for (int i = 1; i < 20; i++)
24                 {
25                     Stopwatch stw = new Stopwatch();
26                     stw.Start();
27                     Random r = new Random();
28                     string result = Respository.SecondFind(data, r.Next(1, 1000)*i);
29                     stw.Stop();
30                     Console.WriteLine(result + "   SecondFindExecuteTime:" + stw.Elapsed.TotalMilliseconds+"ms");
31                 }
32                 stwtotal.Stop();
33                 Console.WriteLine("测试数据总时间:" + stwtotal.Elapsed.TotalMilliseconds + "ms");
34             }
35             {
36                 Stopwatch stwtotal = new Stopwatch();
37                 stwtotal.Start();
38                 Dictionary<int, CityInfo> data = Respository.ThridRetrieve();
39                 for (int i = 1; i < 20; i++)
40                 {
41                     Stopwatch stw = new Stopwatch();
42                     stw.Start();
43                     Random r = new Random();
44                     CityInfo result = Respository.ThridFind(data, r.Next(1, 1000) * i);
45                     stw.Stop();
46                     Console.WriteLine(result.CityName + "   ThridFindExecuteTime:" + stw.Elapsed.TotalMilliseconds + "ms");
47                 }
48                 stwtotal.Stop();
49                 Console.WriteLine("真实数据初始化到查找结束总时间:" + stwtotal.Elapsed.TotalMilliseconds + "ms");
50             }
51             Console.ReadKey();
52         }

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

截图如下:

1.Hashtable

2Dictionary<int,string>

3,Dictionary<int,CityInfo>

 

 

posted @ 2014-02-13 00:40  杉亚  阅读(2055)  评论(0编辑  收藏  举报