确定对 ICollection Count 的调用是否会导致迭代

发布于 2024-11-04 09:54:36 字数 1066 浏览 8 评论 0原文

假设我想从 Collection 访问一个对象,并且我想确定 它不会迭代整个 Collection 只是为了确定大小。

如何确定和控制对 Count 的调用是否会导致实际迭代集合? (除了使用我自己的 ICollection 实现),换句话说,是否有提供此功能的实现?

public void PrintThreeNames(ICollection<string> names)
{
    //first I want to know if the collection has at least three elements.
    if (names != null && names.Count >= 3)
    {
        IEnumerator<string> enumerator = names.GetEnumerator();
        string value0 = enumerator.Current;
        enumerator.MoveNext();
        string value1 = enumerator.Current;
        enumerator.MoveNext();
        string value2 = enumerator.Current;
        //print values.
        Console.Writeline(value0 + value1 + value2);
    }
}

回答一下程序员英雄的问题。我想我可以让 IEnumerable 集合向其中添加一百万个文档,并对其进行计数以了解对 Count 的调用有多快。

我问这个问题是因为我可能会选择使用 IEnumerable 而不是 Collection,因为我的集合的数量和每个项目的数据都非常大,因此返回将是一个问题全部同时进行。

但是,我也想知道 IEnumarable 的缺点,Joshua 在另一个问题中指出锁定它不是一个好主意。

Suppose I want to access an object from a Collection, and I want to be sure
it doesn't iterate through the whole Collection just to determine the size.

How can I determine and control if a call to Count results in actually iterating though the collection?
(other than using my own implementation of ICollection) , in other words are there implementations that offer this?

public void PrintThreeNames(ICollection<string> names)
{
    //first I want to know if the collection has at least three elements.
    if (names != null && names.Count >= 3)
    {
        IEnumerator<string> enumerator = names.GetEnumerator();
        string value0 = enumerator.Current;
        enumerator.MoveNext();
        string value1 = enumerator.Current;
        enumerator.MoveNext();
        string value2 = enumerator.Current;
        //print values.
        Console.Writeline(value0 + value1 + value2);
    }
}

In response to the programmers hero question. I guess I can make a IEnumerable<T> collection add a million documents to it, and count it to see how fast a call to Count is as well.

I asked this question as I may choose using IEnumerable over Collection, as my collections are so large in numbers and data per item as well, that it will be a problem to return all of them at once.

However, I would like to know the disadvantages of IEnumarable as well, Joshua pointed about locking it is not a good idea, in another question.

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

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

发布评论

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

评论(3

哭了丶谁疼 2024-11-11 09:54:36

是否有相对昂贵的 Count 实现?有可能,但这种情况很少见; .NET Framework 类经过调整以获得相当好的全面效率。

有关系吗?几乎可以肯定不是。除非您对包含数百万个元素的集合查询 Count 数百万次,否则差异很小,以至于无关紧要:

  • List.Count 的调用达到 1 亿次包含 100 万个整数:0.85s
  • 1 亿次调用包含 100 万个整数的 HashSet.Count():1.45s

Are there implementations of Count that are relatively expensive? Probably, but they'll be rare; the .NET Framework classes are tuned for pretty good all-round efficiency.

Does it matter? Almost certainly not. Unless you're querying Count millions of times over collections with millions of elements, the difference is so small as to be irrelevant:

  • 100 Million calls to List<T>.Count containing 1 million integers: 0.85s
  • 100 Million calls to HashSet<T>.Count() containing 1 million integers: 1.45s
挽清梦 2024-11-11 09:54:36

因为 ICollection 将 Count 作为属性公开,所以应该可以安全地假设获取其值的成本非常低(即,它不应该迭代整个集合)。

这只是围绕房产的最佳实践 - 获取其价值应该很便宜 - 就这样。

如果操作成本很高,那么它应该是一个方法 - 例如 Count()。

当然,有人肯定可以提供 ICollection.Count 的昂贵实现 - 但是那个人没有做正确的事情。

如果计算元素数量的成本很高,则应仅实现 IEnumerable - 而不是 ICollection。

Because ICollection exposes Count as a property, it should be safe to assume that it is very cheap to get its value (i.e. it should not iterate through the entire collection).

This is just a best practice surrounding properties - getting their values should be cheap - period.

If the operation is expensive, then it should be a method - e.g. Count().

Sure, somebody could certainly provide an expensive implementation of ICollection.Count - however then that person is not doing things right.

If calculating the number of elements is expensive, they should implement only IEnumerable - not ICollection.

浪推晚风 2024-11-11 09:54:36

从技术上讲,因为 ICollection 是一个接口,所以您不能保证它不会迭代该集合。无论如何,该接口都可以实现。

Technically because ICollection is an interface you have no guarantee that it won't iterate through the collection. The interface could be implemented anyway.

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