Loveyuki's BLOG

L-BLOG.NET, The Matrix Reloaded.

.NET 2.0 中 Hashtable 快速查找的方法
Loveyuki | 2007-11-24 16:13 | 991 点击 | 2 评论 | .NET 2.0HashTable

一般来说我们都是用 Hashtable 的 ContainsKey 方法来查找 Hashtable 中是否存在某个键值然后读取他,但是这个方法并不是效率最好的方法。比较好的方法是直接读取键值然后判断这个对象是否为 null 然后读取。两种代码分别如下:

  1. 一般慢速的方法:if (objHash.ContainsKey(keyValue))
    {
        strValue=(String)objHash[keyValue]; 
    }
  2. 而快速的方法是:Object objValue=objHash[keyValue];
    if (objValue!=null)
    {
        strValue=(String)objValue;
    }

两种方法的速度经过测试能差一倍左右。下面是测试代码:
Hashtable objHash = new Hashtable();
for (Int32 intI = 0; intI < 1000; intI++)
{
    objHash.Add("Key_" + intI.ToString(), "Value_" + intI.ToString());
}
String strValue = String.Empty;
Stopwatch timer = new Stopwatch();
timer.Start();
for (Int32 intI = 0; intI < 1000; intI++)
{
    Object objValue = objHash["Key_" + intI.ToString()];
    if (objValue != null)
    {
        strValue = (String)objValue;
    }
}
timer.Stop();
Console.WriteLine("Execution time was {0:F1} microseconds.", timer.Elapsed.Ticks / 10m);
timer.Reset();
timer.Start();
for (Int32 intI = 0; intI < 1000; intI++)
{
    if (objHash.ContainsKey("Key_" + intI.ToString()))
    {
        strValue = (String)objHash["Key_" + intI.ToString()];
    }
}
timer.Stop();
Console.WriteLine("Execution time was {0:F1} microseconds.", timer.Elapsed.Ticks / 10m);
timer.Reset();

测试结果如下:

如果不需要获得对应键的值,而是只判断的话,两种方法的区别不会很明显,大家有空可以测试下。

引用通告:http://www.loveyuki.com/Article/8/Trackback.ashx
引用 星辉一冷
2007-11-24 18:25 #

Good work~

受教了~

引用 ting
2008-04-16 13:14 #

2.0应该使用泛型的hashtable
以下代码运行时间
Execution time was 694511.1 microseconds.
Execution time was 971042.1 microseconds.
Execution time was 92339.1 microseconds.
可见有更强的速度

 Dictionary<string, string> dic = new Dictionary<string, string>();
        Hashtable objHash = new Hashtable();
        for (Int32 intI = 0; intI < 1000000; intI++)
        {
            dic.Add("Key_" + intI.ToString(), "Value_" + intI.ToString());
            objHash.Add("Key_" + intI.ToString(), "Value_" + intI.ToString());
        }
        String strValue = String.Empty;
        Stopwatch timer = new Stopwatch();
        timer.Start();
        for (Int32 intI = 0; intI < 1000000; intI++)
        {
            Object objValue = objHash["Key_" + intI.ToString()];
            if (objValue != null)
            {
                strValue = (String)objValue;
            }
        }
        timer.Stop();
        Debug.WriteLine(string.Format("Execution time was {0:F1} microseconds.", timer.Elapsed.Ticks / 10m));
        timer.Reset();
        timer.Start();
        for (Int32 intI = 0; intI < 1000000; intI++)
        {
            if (objHash.ContainsKey("Key_" + intI.ToString()))
            {
                strValue = (String)objHash["Key_" + intI.ToString()];
            }
        }
        timer.Stop();
        Debug.WriteLine(string.Format("Execution time was {0:F1} microseconds.", timer.Elapsed.Ticks / 10m));
        timer.Reset();

        timer.Start();
        for (Int32 intI = 0; intI < 100000; intI++)
        {
            if (dic.ContainsKey("Key_" + intI.ToString()))
            {
                strValue = dic["Key_" + intI.ToString()];
            }
        }
        timer.Stop();
        Debug.WriteLine(string.Format("Execution time was {0:F1} microseconds.", timer.Elapsed.Ticks / 10m));
        timer.Reset();

(必填)
(必填,不会被公开)
 

版权所有©2007-2008, Loveyuki.com | 系统:Bitrac | 皮肤:Blog Pixel | 空间域名:光辉互联,易联网络