我认为这个片段是 O(n^3) 是正确的吗?

发布于 2024-12-02 08:17:37 字数 318 浏览 0 评论 0原文

collection.Where(i => i.condition)
.ToList()
.ForEach(i => SomeComplicatedOpInvolving_i);

我并不是在寻找答案来告诉我有一种更简单的方法可以做到这一点,只是将其视为一个思想实验。

首先,我认为这是三个循环,对吗? Where()ToList()ForEach()
其次,(假设是三个循环)我认为这是大O表示法中的n的3次方,对吗?

谢谢大家。

collection.Where(i => i.condition)
.ToList()
.ForEach(i => SomeComplicatedOpInvolving_i);

I'm not looking for answers telling me there is an easier way of doing this, just treat it as a thought experiment.

First up, am I right in thinking that this is three loops? Where(), ToList() and ForEach()?
Second of all, (assuming it is three loops) am I right in thinking this is n to the power of 3 in big O notation?

Thanks everyone.

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

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

发布评论

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

评论(6

请叫√我孤独 2024-12-09 08:17:37

不,实际上。我认为应该是O(n)。

Where() 的运行时间复杂度为 O(n)(假设 condition 为常数)

ToList() 运行 Where< 的所有结果/code> 也是如此,所以 O(n) 也是

ForEach() 运行一次 ToList 生成的整个列表,所以也是 O(n)。我假设 SomeComplicatedOpInvolving_i 不会随着 i 的数量而缩放...

这里的关键点是循环不是嵌套的,它们一个接一个地运行 - 所以总运行时间是 3*O(n ),与 O(n) 相同。

No, actually. I think it should be O(n).

Where() runs in O(n) (assuming condition is constant)

ToList() runs over all the results of Where as well, so O(n) too

ForEach() runs over the entire list produced by ToList once, so also O(n). I assume SomeComplicatedOpInvolving_i doesn't scale with the number of i's...

The key point here is that the loops aren't nested, they run one after another - so total runtime is 3*O(n), which is the same as O(n).

假装爱人 2024-12-09 08:17:37

不,它们不是嵌套循环。是O(n)。

避免使用 ToList(),这会无缘无故地消耗 O(n) 存储空间。查看此博客文章

No, they are not nested loops. It is O(n).

Avoid using ToList(), that costs O(n) storage for no good reason. Check this blog post.

伪心 2024-12-09 08:17:37

不,它是O(n)。如果循环内有循环,则复杂度仅为O(n^3)

No, it's O(n). It would only be O(n^3) if there was a loop within a loop within a loop.

机场等船 2024-12-09 08:17:37

这是O(n)

由于 WhereO(n),这意味着 Where 的成本约为 A x n,对于某些 <代码>A。

ToListForEach 类似,也是 O(n) 这意味着它们的成本约为 B x n对于一些 B 和一些 C 分别为 >C x n

这意味着总成本大致为:

  (A x n) + (B x n) + (C x n)
= (A + B + C) x n 
= D x n

对于某些 D(我们从来没有真正关心过 ABC > 是,所以我们也不关心 A + B + C 是什么,所以只需将其称为 D 以使我们的方程更简单)。

因此这个操作的复杂度是O(n)

This is O(n).

As Where is O(n) this means that the cost of Where is approximately A x n, for some A.

Similarly as ToList and ForEach are also O(n) which means their cost is approximately B x n and C x n respective, for some B and some C.

This means that the total cost is roughly:

  (A x n) + (B x n) + (C x n)
= (A + B + C) x n 
= D x n

For some D (we never really cared what A, B and C were, so we also don't care what A + B + C is, so just call it D to make our equation simpler).

Hence this operation is O(n).

遥远的绿洲 2024-12-09 08:17:37

它是 O(collection.Size) * O(SomeComplicatedOpInvolving_i)。

It's O(collection.Size) * O(SomeComplicatedOpInvolving_i).

饭团 2024-12-09 08:17:37
collection.Where(i => i.condition) // is O(n) where n is the size of collection
.ToList() // is O(m) where m is the number if items with i.condition true
.ForEach(i => SomeComplicatedOpInvolving_i);  // O(m) where m is the number if items with i.condition true

假设 SomeComplicatedOpInvolving 的复杂度为 O(1),例如,如果您有更多项目,它不会变得更长。

鉴于 那么

when m is never bigger then n, then 
O(n) + O(m) + O(m) == O(n) 

该片段是 O(n)

collection.Where(i => i.condition) // is O(n) where n is the size of collection
.ToList() // is O(m) where m is the number if items with i.condition true
.ForEach(i => SomeComplicatedOpInvolving_i);  // O(m) where m is the number if items with i.condition true

Assuming that SomeComplicatedOpInvolving is O(1), e.g. it does not make longer if you have more items.

Given that

when m is never bigger then n, then 
O(n) + O(m) + O(m) == O(n) 

Then the snippet is O(n)

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