Hashtable和Dictionary性能比较

来源:互联网 发布:namecheap域名转出 编辑:程序博客网 时间:2024/05/16 05:26

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

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

输出为:

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对效率没什么影响。