迭代HashSet的最快/最安全的方法是什么? [英] What is the fastest/safest method to iterate over a HashSet?

查看:110
本文介绍了迭代HashSet的最快/最安全的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是C#的新手,但通过论坛发帖使用 HashSet 而不是列表在特定情况下。

I'm still quite new to C#, but noticed the advantages through forum postings of using a HashSet instead of a List in specific cases.

我当前的情况不是我在一个列表中存储了大量数据 exectly,但不是我经常要检查它的成员。

My current case isn't that I'm storing a tremendous amount of data in a single List exectly, but rather than I'm having to check for members of it often.

我确实需要迭代它作为好吧,但它们存储或检索的顺序实际上并不重要。

The catch is that I do indeed need to iterate over it as well, but the order they are stored or retrieved doesn't actually matter.

我读过每个循环实际上比下一个慢,所以还有什么其他的我用尽可能快的方法来解决这个问题?

I've read that for each loops are actually slower than for next, so how else could I go about this in the fastest method possible?

.Contains()的数量检查我在做什么肯定会损害我的列表性能,所以至少与 HashSet 的性能比较会很方便。

The number of .Contains() checks I'm doing is definitely hurting my performance with lists, so at least comparing to the performance of a HashSet would be handy.

编辑:我目前正在使用列表,在许多位置迭代它们,并且正在执行不同的代码在每个位置。大多数情况下,当前列表包含点坐标,然后我用它来引用二维数组,然后根据列表的条件执行某些操作或其他操作。

I'm currently using lists, iterating through them in numerous locations, and different code is being executed in each location. Most often, the current lists contain point coordinates that I then use to refer to a 2 dimensional array for that I then do some operation or another based on the criteria of the list.

如果我的问题没有直接答案,那很好,但我认为可能有其他方法迭代 HashSet 而不仅仅是 foreach 循环。我目前处于黑暗状态,甚至可能有其他方法,它们提供了哪些优势等等。假设还有其他方法,我还假设有一种典型的首选方法,只有在它不能满足需求(我的需求非常基本)。

If there's not a direct answer to my question, that's fine, but I assumed there might be other methods of iterating over a HashSet than just foreach cycle. I'm currently in the dark as to what other methods there might even be, what advantages they provide, etc. Assuming there are other methods, I also made the assumption that there would be a typical preferred method of choice that is only ignored when it doesn't suite the needs (my needs are pretty basic).

就过早优化而言,我已经知道使用列表,因为我是一个瓶颈。如何解决这个问题是我陷入困境的地方。甚至没有完全卡住,但我不想通过重复测试来重新发明轮子只是为了发现我已经尽力做到这一点(这是一个投资超过3个月的大型项目,列表无处不在,但肯定有一些我不想重复,有大量数据,不需要以任何特定顺序存储等等。)

As far as prematurely optimizing, I already know using the lists as I am is a bottleneck. How to go about helping this issue is where I'm getting stuck. Not even stuck exactly, but I didn't want to re-invent the wheel by testing repeatedly only to find out I'm already doing it the best way I could (this is a large project with over 3 months invested, lists are everywhere, but there are definitely ones that I do not want duplicates, have a lot of data, need not be stored in any specific order, etc).

推荐答案

foreach循环在索引集合(如数组)上有少量的额外开销。
这主要是因为foreach比for循环更多地进行边界检查。

A foreach loop has a small amount of addition overhead on an indexed collections (like an array). This is mostly because the foreach does a little more bounds checking than a for loop.

HashSet没有索引器,所以你必须使用枚举器。

HashSet does not have an indexer so you have to use the enumerator.

在这种情况下,foreach是高效的,因为它只是在移动集合时调用MoveNext()。

In this case foreach is efficient as it only calls MoveNext() as it moves through the collection.

此外,Parallel.ForEach可以显着提高您的性能,具体取决于您在循环中所做的工作以及HashSet的大小。

Also Parallel.ForEach can dramatically improve your performance, depending on the work you are doing in the loop and the size of your HashSet.

如前所述,分析是最好的选择。

As mentioned before profiling is your best bet.

这篇关于迭代HashSet的最快/最安全的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆