哪个更快/更高效:Dictionary或字典<枚举 ,对象>?

发布于 2024-07-18 07:26:16 字数 616 浏览 4 评论 0原文

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

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

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

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

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

以及

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

如果用作字典中的键,以上哪一个会更好!

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慕巷 2024-07-25 07:26:16

当然,enum 版本更好(当然,当两者都适用并且有意义时)。 不仅仅是为了性能(它可以更好也可以更差,请参阅 Rashack 的非常好的评论),因为它会检查编译时间并产生更干净的代码。

您可以通过使用 Dictionary 并将 enum 键转换为 int 或指定自定义比较器来规避比较器问题。

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.

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

半世蒼涼 2024-07-25 07:26:16

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

在此过程的后期使代码更快通常是一个直接的过程。 使用 skolima 提供的链接。 如果您选择了枚举,则大约需要 10 分钟的时间才能消除应用程序中的潜在性能问题。 我想在这里强调“潜力”这个词。 这对于 NHibernate 来说绝对是一个问题,但是对于你的程序来说这是否会是一个问题完全取决于用途。

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

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.

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.

下壹個目標 2024-07-25 07:26:16

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

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 .

爱,才寂寞 2024-07-25 07:26:16

我猜想枚举版本更快。 在底层,字典通过哈希码引用所有内容。 我的猜测是,为字符串生成哈希码的速度较慢。 然而,这可能慢得可以忽略不计,而且肯定比字符串比较之类的东西更快。 我同意其他发帖者的观点,他们说枚举更干净。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文