Directory 与 Hashtable 的性能比较

  

在.net1.1里经常会使用到Hashtable,到里.net 2.0以后我发现有了一个很好用的IDictionary<TKey,TValue>实现类Dictionary<TKey,TValue>。但还是会担心Dictionary<TKey,TValue>的检索效率是否跟Hashtable相当,

据我了解ArrayList的检索效率是非常差的,BinarySearch也不如Hashtable.所以做了一个测试。

[c-sharp] view plain copy print ?
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         IntTest();  
  6.   
  7.         StringTest();  
  8.   
  9.         IntTryGetValueTest();  
  10.   
  11.         Console.WriteLine("=========");  
  12.   
  13.         IntTest();  
  14.   
  15.         StringTest();  
  16.   
  17.         IntTryGetValueTest();  
  18.   
  19.         Console.ReadLine();  
  20.     }  
  21.   
  22.     static void IntTest()  
  23.     {  
  24.         IDictionary<intint> id = new Dictionary<intint>();  
  25.   
  26.         Hashtable ht = new Hashtable();  
  27.   
  28.         int count = 1000000;  
  29.   
  30.         for (int i = 0; i < count; i++)  
  31.         {  
  32.             id.Add(i, i);  
  33.   
  34.             ht.Add(i, i);  
  35.         }  
  36.         //   
  37.         Stopwatch st = Stopwatch.StartNew();  
  38.   
  39.         for (int i = 0; i < count; i++)  
  40.         {                 
  41.             int str = id[i];                  
  42.         }  
  43.   
  44.         st.Stop();  
  45.   
  46.         Console.WriteLine(string.Format("int类型,Directory:{0}", st.ElapsedMilliseconds));  
  47.         //   
  48.         st = Stopwatch.StartNew();  
  49.   
  50.         for (int i = 0; i < count; i++)  
  51.         {  
  52.             object obj = ht[i];  
  53.         }  
  54.   
  55.         st.Stop();  
  56.   
  57.         Console.WriteLine(string.Format("int类型,Hashtable:{0}", st.ElapsedMilliseconds));  
  58.     }  
  59.   
  60.     static void StringTest()  
  61.     {  
  62.         IDictionary<stringstring> id = new Dictionary<stringstring>();  
  63.   
  64.         Hashtable ht = new Hashtable();  
  65.   
  66.         int count = 1000000;  
  67.   
  68.         for (int i = 0; i < count; i++)  
  69.         {  
  70.             string str = i.ToString();  
  71.   
  72.             id.Add(str, str);  
  73.   
  74.             ht.Add(str, str);  
  75.         }  
  76.         //   
  77.         Stopwatch st = Stopwatch.StartNew();  
  78.   
  79.         for (int i = 0; i < count; i++)  
  80.         {               
  81.            string i2 = id[i.ToString()];                  
  82.         }  
  83.   
  84.         st.Stop();  
  85.   
  86.         Console.WriteLine(string.Format("string类型,Directory:{0}", st.ElapsedMilliseconds));  
  87.         //   
  88.         st = Stopwatch.StartNew();  
  89.   
  90.         for (int i = 0; i < count; i++)  
  91.         {  
  92.             object obj = ht[i.ToString()];  
  93.         }  
  94.   
  95.         st.Stop();  
  96.   
  97.         Console.WriteLine(string.Format("string类型,Hashtable:{0}", st.ElapsedMilliseconds));  
  98.   
  99.     }  
  100.   
  101.     static void IntTryGetValueTest()  
  102.     {  
  103.         IDictionary<intint> id = new Dictionary<intint>();  
  104.   
  105.         Hashtable ht = new Hashtable();  
  106.   
  107.         int count = 1000000;  
  108.   
  109.         for (int i = 0; i < count; i++)  
  110.         {  
  111.             id.Add(i, i);  
  112.   
  113.             ht.Add(i, i);  
  114.         }  
  115.         //   
  116.         Stopwatch st = Stopwatch.StartNew();  
  117.   
  118.         for (int i = 0; i < count; i++)  
  119.         {  
  120.             int i2 = 0;  
  121.   
  122.             id.TryGetValue(i, out i2);  
  123.   
  124.         }  
  125.   
  126.         st.Stop();  
  127.   
  128.         Console.WriteLine(string.Format("IntTryGetValue,Directory:{0}", st.ElapsedMilliseconds));  
  129.         //   
  130.         st = Stopwatch.StartNew();  
  131.   
  132.         for (int i = 0; i < count; i++)  
  133.         {  
  134.             object obj = ht[i];  
  135.         }  
  136.   
  137.         st.Stop();  
  138.   
  139.         Console.WriteLine(string.Format("int类型,Hashtable:{0}", st.ElapsedMilliseconds));  
  140.     }  
  141. }  

输出为:

int类型,Directory:49
int类型,Hashtable:254
string类型,Directory:1112
string类型,Hashtable:511
IntTryGetValue,Directory:50
int类型,Hashtable:251
=========
int类型,Directory:48
int类型,Hashtable:201
string类型,Directory:944
string类型,Hashtable:505
IntTryGetValue,Directory:51
int类型,Hashtable:167

~~~~~~~~~~~~~~~~~~·

从结果我们可以发现如果key是整数型Dictionary的效率比Hashtable快3到4倍,

如果key是字符串型,Dictionary的效率只有Hashtable的一半。

另外使用TryGetValue对效率没什么影响。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值