近期做版本疊代任務,有一個在店鋪頭部展示店主所在的城市名稱和省份名稱的需求,店主資訊表中儲存了店主所在的城市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>