First() 会执行 OrderBy() 吗?
之间的(渐近)性能是否有任何差异,
var a = Orders.OrderBy(order => order.Date).First()
和
var y = Orders.Where(order => order.Date == Orders.Min(x => x.Date)).ToList();
即 First() 是否会执行 OrderBy()?我猜不会。 MSDN 说通过 foreach och GetEnumerator 枚举集合,但措辞没有排除其他扩展名。
Is there any difference in (asymptotic) performance between
var a = Orders.OrderBy(order => order.Date).First()
and
var y = Orders.Where(order => order.Date == Orders.Min(x => x.Date)).ToList();
i.e. will First() perform the OrderBy()? I'm guessing no. MSDN says enumerating the collection via foreach och GetEnumerator does but the phrasing does not exclude other extensions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有几件事:
OrderBy()
从小到大排序,因此您的两个替代方案返回不同的元素Where()
通常是惰性的,因此您的第二个表达式实际上并不执行此操作任何计算都没有 - 直到使用为止。First()
识别(在编译时或运行时)它正在有序枚举上运行,并且选择返回(第一个)最小元素,而不是排序。IEnumerable
提供程序,OrderBy
恰好返回一个枚举,每次检索第一个元素时,它都会对输入进行完全缓冲和排序 - 因此,在常见的基本情况中Linq-to-objects 情况下,OrderBy().First()
与OrderBy().ToArray()
相当。请记住,linq 只是一堆函数名称 - 每个提供程序可能会选择以不同的方式实现这些名称,因此上述内容仅适用于 System.Linq IEnumerable 查询提供程序,而不一定适用于其他提供程序。
A few things:
OrderBy()
orders from small to large, so your two alternatives return different elementsWhere()
is typically lazy, so your second expression doesn't actually do any computation at all - not until used.First()
on it recognizes (either at compile or run-time) that it's running on an ordered enumerable and instead of sorting, opts to return the (first) minimum element.IEnumerable<T>
provider,OrderBy
happens to return an enumerable that fully buffers and sorts the input each time the first element is retrieved - so, in the common basic Linq-to-objects case,OrderBy().First()
is comparable toOrderBy().ToArray()
.Remeber that linq is just a bunch of function names - each provider may choose to implement these differently, so the above only holds for the System.Linq IEnumerable query provider, and not necessarily others.
First
将返回传递给它的 IEnumerable 的第一个条目。由于传递给First
的 IEnumerable 是OrderBy
的结果,因此您的问题可以改写为“OrderBy
有效吗”,是的,它确实有效。First
无法推迟OrderBy
的执行,因为它会立即返回结果。例如:First
will return the first entry of the IEnumerable passed to it. Since the IEnumerable passed toFirst
is the result ofOrderBy
your question can be rephrased to "DoesOrderBy
work", and, yes it does.First
cannot defer the execution ofOrderBy
because it returns the result right away. For example:First
方法将执行OrderBy
(也就是说,当然是执行First
方法)。当First
方法从OrderBy
结果中提取第一个项目时,它必须对所有项目进行排序以找出哪个是第一个。根据查询的运行位置和方式(即,如果查询引擎无法围绕它进行优化),第二个查询的性能可能会非常糟糕。如果为
Orders
中的每个项目计算一次Orders.Max
,它就会变成 O(n*n) 操作,这是非常糟糕的。还有一个功能差异,如果存在重复的日期,第二个查询可以返回多个项目。
The
First
method will perform theOrderBy
(that is, given that theFirst
method is executed of course). When theFirst
method pulls the first item from the result of theOrderBy
, it will have to sort all the items to find out which one is the first one.Depending on where and how the query is run (i.e. if the query engine can't optimise around it), the second query can perform quite badly. If
Orders.Max
is evaluated once for each item inOrders
, it becomes an O(n*n) operation, which is pretty bad.There is a functional difference also, the second query can return more than one item if there are duplicate dates.
事实并非如此。话虽这么说,orderby 自然会在有人尝试实际获取第一个元素时执行。
但正如你所说,条件可能会进一步确定。因此,不 - 它当时不会执行。
It does not. THAT BEING SAID - naturally the orderby will execute the moment someone tries to actually GET the first element.
But as you said, the conditions may be further defined. As such, no - it does not execute at that moment.