为按键查找Dictionary元素选择哪个方法

本文关键字:选择 方法 元素 Dictionary 查找 | 更新日期: 2023-09-27 18:08:48

我想知道,在速度方面,以下哪种方法更可取?

//Dictionary dic<string, int>;
int getElementByKey1(string key)
{
    if(dic.ContainsKey(key))     //Look-up 1
        return dic[key];     //Look-up 2 in case of a "hit"
    return null;
}
int getElementByKey2(string key)
{
    try
    {
        return dic[key];      //Single look-up in case of a "hit"
    }
    catch
    {
        return null;          //Exception in case of a "miss"
    }
}

为按键查找Dictionary元素选择哪个方法

第三个呢,使用TryGetValue()方法:

int getElementByKey3(string key)
{
    int value;
    dic.TryGetValue(key, out value)
    return value;
}

顺便说一下:你的方法是无效的,因为你不能从声明为int的方法返回null

应该声明为int?,而不是允许null值:

int? getElementByKey3(string key)
{
    int value;
    if(dic.TryGetValue(key, out value))
        return value;
    return null;
}

我认为这是最好的。但是,如果我必须从你建议的两个方法中选择一个,我会选择第一个-秒看起来更快,但是当异常将被抛出时,它不会那么快,因为它必须被处理,它需要一些工作。

您可以使用StopWatchers测试执行时间,首先,在Dictionary上放一些值:

    Random r = new Random();
    for (int i = 0; i < 10000; i++)
    {
        d.Add(Guid.NewGuid().ToString(), r.Next());
        //put some manual key for search later
        if (i == 9001)
            d.Add("it's over", 9000);
    }

然后,使用StopWatchers(使用System.Diagnostics)进行一些搜索:

  • 第一次测试,当值存在时(不抛出异常):

    Stopwatch st1 = new Stopwatch();
    st1.Start();
    int getKey1 = getElementByKey1("it's over");
    st1.Stop();
    Stopwatch st2 = new Stopwatch();
    st2.Start();
    int getKey2 = getElementByKey2("it's over");
    st2.Stop();
    

Result in my pc:

Time spent on first search: 00:00:00.0002738
Time spent on second search: 00:00:00.0001169

所以,第一个比较慢,因为在返回值之前验证if (d.ContainsKey(key))

  • 第二次测试,当值不存在(抛出异常,例如:int getKey1 = getElementByKey1("nevermind");):

结果:

Time spent on first search: 00:00:00.0002331
Time spent on second search: 00:00:00.0822669

可以看到,抛出异常会降低性能。

注意:你不能在返回int的方法上返回null,所以我使用return 0;

无。最好是:

string result = null;
if (dic.TryGetValue(key, out result)
{
    // don't know, maybe log it somewhere or sth?
}
return result;