哪个更快/更有效:Dictionary 或Dictionary ?

时间:2022-07-11 01:12:46

Are enum types faster/more efficient than string types when used as dictionary keys?

当用作字典键时,枚举类型是否比字符串类型更快/更有效?

IDictionary<string,object> or IDictionary<enum,object>

As a matter of fact, which data type is most suitable as a dictionary key and why?

事实上,哪种数据类型最适合作为字典键,为什么?

Consider the following: NOTE: Only 5 properties for simplicity

请考虑以下事项:注意:为简单起见,只有5个属性

struct MyKeys
{
   public string Incomplete = "IN"; 
   public string Submitted = "SU"; 
   public string Processing="PR"; 
   public string Completed = "CO"; 
   public string Closed = "CL";   
}

and

enum MyKeys
{
   Incomplete, 
   Submitted, 
   Processing, 
   Completed, 
   Closed
}

Which of the above will be better if used as keys in a dictionary!

如果在字典中用作键,上面哪个会更好!

5 个解决方案

#1


Certainly the enum version is better (when both are applicable and make sense, of course). Not just for performance (it can be better or worse, see Rashack's very good comment) as it's checked compile time and results in cleaner code.

当然,枚举版本更好(当两者都适用时,当然有意义)。不仅仅是为了性能(可能更好或更糟,请参阅Rashack的非常好的评论),因为它检查了编译时间并产生更清晰的代码。

You can circumvent the comparer issue by using Dictionary<int, object> and casting enum keys to ints or specifying a custom comparer.

您可以通过使用Dictionary 并将枚举键转换为整数或指定自定义比较器来绕过比较器问题。 ,object>

#2


I think you should start by focusing on correctness. This is far more important than the minimal difference between the minor performance differences that may occur within your program. In this case I would focus on the proper representation of your types (enum appears to be best). Then later on profile your application and if there is a issue, then and only then should you fix it.

我认为你应该首先关注正确性。这比程序中可能出现的次要性能差异之间的最小差异重要得多。在这种情况下,我将专注于您的类型的正确表示(枚举似乎是最好的)。然后稍后对您的应用程序进行概要分析,如果出现问题,那么只有这样才能修复它。

Making code faster later in the process is typically a straight forward process. Take the link that skolima provided. If you had chosen enum, it would have been a roughly 10 minute fix to remove a potential performance problem in your application. I want to stress the word potential here. This was definitely a problem for NHibernate but as to whether or not it would be a problem for your program would be solely determined by the uses.

在此过程中稍后加快代码通常是一个简单的过程。获取skolima提供的链接。如果您选择了枚举,那么在应用程序中删除潜在的性能问题大概需要10分钟。我想在这里强调潜力这个词。对于NHibernate来说这肯定是一个问题,但是对于你的程序来说它是否会成为问题将完全取决于用途。

On the other hand, making code more correct later in the process tends to be more difficult. In a large enough problem you'll find that people start taking dependencies on the side effects of the previous bad behavior. This can make correcting code without breaking other components challenging.

另一方面,在过程的后期使代码更正确往往更加困难。在一个足够大的问题中,你会发现人们开始依赖于先前不良行为的副作用。这可以在不破坏其他组件的情况下纠正代码。

#3


Use enum to get cleaner and nicer code, but remember to provide a custom comparer if you are concerned with performance: http://ayende.com/Blog/archive/2009/02/21/dictionaryltenumtgt-puzzler.aspx .

使用枚举来获得更清晰,更好的代码,但如果您关注性能,请记得提供自定义比较器:http://ayende.com/Blog/archive/2009/02/21/dictionaryltenumtgt-puzzler.aspx。

#4


May not apply but...

可能不适用但......

Be aware that enums are compiled as constants which may cause redeployment of all assemblies which reference the enum if it is altered. (i.e. the constant is hardcoded in at compile time to all assemblies that use it).

请注意,枚举被编译为常量,这可能会导致重新部署引用枚举的所有程序集(如果已更改)。 (即,在编译时将常量硬编码到使用它的所有程序集)。

#5


I would guess that the enum version is faster. Under the hood the dictionary references everything by hashcode. My guess is that it is slower to generate the hashcode for a string. However, this is probably negligibly slower, and is most certainly faster than anything like a string compare. I agree with the other posters who said that an enum is cleaner.

我猜想枚举版本更快。在引擎盖下,字典通过哈希码引用所有内容。我的猜测是,为字符串生成哈希码的速度较慢。然而,这可能是可以忽略的慢,并且肯定比字符串比较更快。我同意其他海报的说法,说枚举更清洁。

#1


Certainly the enum version is better (when both are applicable and make sense, of course). Not just for performance (it can be better or worse, see Rashack's very good comment) as it's checked compile time and results in cleaner code.

当然,枚举版本更好(当两者都适用时,当然有意义)。不仅仅是为了性能(可能更好或更糟,请参阅Rashack的非常好的评论),因为它检查了编译时间并产生更清晰的代码。

You can circumvent the comparer issue by using Dictionary<int, object> and casting enum keys to ints or specifying a custom comparer.

您可以通过使用Dictionary 并将枚举键转换为整数或指定自定义比较器来绕过比较器问题。 ,object>

#2


I think you should start by focusing on correctness. This is far more important than the minimal difference between the minor performance differences that may occur within your program. In this case I would focus on the proper representation of your types (enum appears to be best). Then later on profile your application and if there is a issue, then and only then should you fix it.

我认为你应该首先关注正确性。这比程序中可能出现的次要性能差异之间的最小差异重要得多。在这种情况下,我将专注于您的类型的正确表示(枚举似乎是最好的)。然后稍后对您的应用程序进行概要分析,如果出现问题,那么只有这样才能修复它。

Making code faster later in the process is typically a straight forward process. Take the link that skolima provided. If you had chosen enum, it would have been a roughly 10 minute fix to remove a potential performance problem in your application. I want to stress the word potential here. This was definitely a problem for NHibernate but as to whether or not it would be a problem for your program would be solely determined by the uses.

在此过程中稍后加快代码通常是一个简单的过程。获取skolima提供的链接。如果您选择了枚举,那么在应用程序中删除潜在的性能问题大概需要10分钟。我想在这里强调潜力这个词。对于NHibernate来说这肯定是一个问题,但是对于你的程序来说它是否会成为问题将完全取决于用途。

On the other hand, making code more correct later in the process tends to be more difficult. In a large enough problem you'll find that people start taking dependencies on the side effects of the previous bad behavior. This can make correcting code without breaking other components challenging.

另一方面,在过程的后期使代码更正确往往更加困难。在一个足够大的问题中,你会发现人们开始依赖于先前不良行为的副作用。这可以在不破坏其他组件的情况下纠正代码。

#3


Use enum to get cleaner and nicer code, but remember to provide a custom comparer if you are concerned with performance: http://ayende.com/Blog/archive/2009/02/21/dictionaryltenumtgt-puzzler.aspx .

使用枚举来获得更清晰,更好的代码,但如果您关注性能,请记得提供自定义比较器:http://ayende.com/Blog/archive/2009/02/21/dictionaryltenumtgt-puzzler.aspx。

#4


May not apply but...

可能不适用但......

Be aware that enums are compiled as constants which may cause redeployment of all assemblies which reference the enum if it is altered. (i.e. the constant is hardcoded in at compile time to all assemblies that use it).

请注意,枚举被编译为常量,这可能会导致重新部署引用枚举的所有程序集(如果已更改)。 (即,在编译时将常量硬编码到使用它的所有程序集)。

#5


I would guess that the enum version is faster. Under the hood the dictionary references everything by hashcode. My guess is that it is slower to generate the hashcode for a string. However, this is probably negligibly slower, and is most certainly faster than anything like a string compare. I agree with the other posters who said that an enum is cleaner.

我猜想枚举版本更快。在引擎盖下,字典通过哈希码引用所有内容。我的猜测是,为字符串生成哈希码的速度较慢。然而,这可能是可以忽略的慢,并且肯定比字符串比较更快。我同意其他海报的说法,说枚举更清洁。