且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么检查字典是否包含密钥更快,而不是捕获异常,以防万一它不?

更新时间:2023-01-13 17:01:37

一方面,抛出异常本质上是昂贵的,因为堆栈必须是解除等等

另一方面,通过密钥访问字典中的值是便宜的,因为它是一个快速的O(1)操作。



BTW:正确的方法是使用 TryGetValue

  obj item; 
if(!dict.TryGetValue(name,out item))
return null;
返回项目;

这只访问一次,而不是两次。

如果你真的想要返回 null 如果该键不存在,则可以进一步简化上述代码:

  obj item; 
dict.TryGetValue(name,out item);
返回项目;

因为 TryGetValue 设置 item to null 如果没有名称的键存在。 >

Imagine the code:

public class obj
{
    // elided
}

public static Dictionary<string, obj> dict = new Dictionary<string, obj>();

Method 1

public static obj FromDict1(string name)
{
    if (dict.ContainsKey(name))
    {
        return dict[name];
    }
    return null;
}

Method 2

public static obj FromDict2(string name)
{
    try
    {
        return dict[name];
    }
    catch (KeyNotFoundException)
    {
        return null;
    }
}

I was curious if there is a difference in performance of these 2 functions, because the first one SHOULD be SLOWER than second one - given that it needs to check twice if the dictionary contains a value, while second function does need to access the dictionary only once but WOW, it's actually opposite:

Loop for 1 000 000 values (with 100 000 existing and 900 000 non existing):

first function: 306 milliseconds

second function: 20483 milliseconds

Why is that?

EDIT: As you can notice in comments below this question, the performance of second function is actually slightly better than first one in case there are 0 non existing keys. But once there is at least 1 or more non existing keys, the performance of second one decrease rapidly.

On the one hand, throwing exceptions is inherently expensive, because the stack has to be unwound etc.
On the other hand, accessing a value in a dictionary by its key is cheap, because it's a fast, O(1) operation.

BTW: The correct way to do this is to use TryGetValue

obj item;
if(!dict.TryGetValue(name, out item))
    return null;
return item;

This accesses the dictionary only once instead of twice.
If you really want to just return null if the key doesn't exist, the above code can be simplified further:

obj item;
dict.TryGetValue(name, out item);
return item;

This works, because TryGetValue sets item to null if no key with name exists.