Linq、lambda - 哪个语句更快?

发布于 2024-10-11 03:00:47 字数 354 浏览 3 评论 0原文

我想知道哪种表达更快,哪种表达更受欢迎:

myList.Select(a => a.Property)
      .Where(a => !String.IsNullOrEmpty(a))

myList.Where(a => !String.IsNullOrEmpty(a.Property))
      .Select(a => a.Property)

当然为什么?

一般来说,我的问题是:我应该使用 Where 后跟 Select 还是 Select 后跟 Where

I was wondering which expression is faster and which one is preferred:

myList.Select(a => a.Property)
      .Where(a => !String.IsNullOrEmpty(a))

myList.Where(a => !String.IsNullOrEmpty(a.Property))
      .Select(a => a.Property)

and of course why?

Generally my question is: should I use Where followed by Select or Select followed by Where?

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

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

发布评论

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

评论(4

故笙诉离歌 2024-10-18 03:00:47

没有人能知道,你必须衡量。考虑一个包含 50 个项目的列表,其中 40 个项目满足过滤条件。

项目然后过滤,这种方法最大限度地减少了对a.Property的访问次数。 100 次匿名方法调用和 50 次属性访问。

myList
   .Select(a => a.Property)
   .Where(a => !String.IsNullOrEmpty(a))

过滤然后投影,这种方法最大限度地减少了对匿名方法的调用次数。 90 次匿名方法调用和 90 次属性访问。

myList
   .Where(a => !String.IsNullOrEmpty(a.Property))
   .Select(a => a.Property) 

由于我们不知道您的属性的实现成本与匿名方法调用的成本,因此无法推断性能差异。

No one can know, you must measure. Consider a list of 50 items, with 40 items meeting the filter criteria.

Project then filter, this approach minimizes the number of accesses to a.Property. 100 anonymous method invocations and 50 property accesses.

myList
   .Select(a => a.Property)
   .Where(a => !String.IsNullOrEmpty(a))

Filter then project, this approach minimizes the number of calls to anonymous methods. 90 anonymous method invocations and 90 property accesses.

myList
   .Where(a => !String.IsNullOrEmpty(a.Property))
   .Select(a => a.Property) 

Since we don't know the costs of your property's implementation vs the costs of an anonymous method invocation, there's no way to reason about the performance difference.

随遇而安 2024-10-18 03:00:47

我更喜欢第二种,首先过滤数据(使用 Where),然后选择(使用 Select)您想要的数据。

根据您要过滤的数据,性能可能会有所不同,但我觉得第二个更流畅。

I would prefer the second one, where you filter the data first (using Where) and then select (using Select) what data you want.

Depending on the data you are filtering performance may vary, but I feel the second one is more in flow.

三月梨花 2024-10-18 03:00:47

这取决于 Linq 提供商。

例如,在 Linq2Sql 中,两个语句是相同的,因为 thwy 将针对数据库生成相同的 SQL。

在 Linq2Objects 中,它的执行方式可能有所不同。

It depends on the Linq provider.

For example in Linq2Sql both statements are the same since thwy will generate the same SQL against the database.

In Linq2Objects it may perform diferently.

没关系。在上述情况下,“查询”的其余部分仅引用投影数据,您也可以先投影,然后过滤。

请注意,并不是先过滤就一定更快;而是先过滤。通过首先调用Select,您将执行更少的属性访问器 - 但无论哪种方式,差异可能都很小。

如果您要编写更复杂的查询,我建议尽快缩小数据范围,在本例中这意味着在 WhereSelect >:这会导致代码较短,在较长的查询中更具可读性:毕竟,通过仅关注相关位(此处为字符串),读者可以忽略查询其余部分中包含它们的更复杂的对象。然而,对于如此小的查询来说,这个优势是相当没有意义的。

It doesn't much matter. In cases such as the above, where the remainder of the "query" only references the projected data, you may as well project first and then filter.

Note that it's not the case that filtering first is necessarily faster; by calling Select first you will be executing fewer property accessors - but the difference is likely to be minor either way.

If you're going to write more complex queries, I suggest writing reducing the scope of the data as quickly as you can, which in this case means writing the Select before the Where: that results in shorter code which is more readable in longer queries: after all, by focusing only on the relevant bits (here strings) the reader can ignore the more complex objects containing them in the rest of the query. This advantage is fairly pointless for such a small query, however.

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