LINQ版本比foreach版本更快吗? [英] Is the LINQ version faster than the foreach one?

查看:91
本文介绍了LINQ版本比foreach版本更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LINQ版本是否比foreach版本更快?

Is the LINQ version faster than the foreach one?

推荐答案

哇!这不是一个公平的问题 - 它不是比较两个类似的操作。

foreach 是一个线性操作:除非由于某种原因它过早地离开循环,它一个接一个地在集合的每个元素上运行相同的代码(循环体)。



Linq操作不这样做:实际上Linq操作不是执行直到需要结果 - 所以只比较一个Linq方法和等效的foreach循环是无效的,因为后者将总是花费代码持续时间的n倍,但Linq方法可能需要花费很少的时间来设置操作然后立即返回而不执行循环体代码。



但是,即使你迫使Linq立即执行完成,它也是可能会稍微慢一点(或者在某些情况下要慢得多),因为它引入了幕后的开销 - 并且很容易编写Linq代码(或Linq方法),这需要更长的时间如果你不注意你正在做的事情,这是一个等价的foreach ......



使用代码中最清晰,最合适的东西;当你有一个比较的具体例子时,只关心哪个是最快的。然后看看你如何非常仔细地计时! :笑:
Whoa! That's not a fair question - it's not comparing two similar operations.
foreach is a linear operation: unless it leaves the loop prematurely for some reason, it runs the same code (the loop body) on each element of the collection one after the other.

Linq operations do not do that: in fact Linq operations are not executed until the result is needed - so just comparing a Linq method with the equivalent foreach loop is not valid as the later will always take "n" times the code duration, but the Linq method may take a trivial amount of time to set up the operation and then return immediately without executing the "loop body" code at all.

But, even if you "force" the Linq to execute to completion immediately, it is likely to be marginally slower, (or considerably slower in some cases) because of overheads it introduces "behind the scenes" - and it is very easy to write Linq code (or Linq methods) which take longer than an equivalent foreach if you do not pay attention to what you are doing...

Use what it clearest and most appropriate in the code; only worry about "which is fastest" when you have a concrete example to compare. And then watch how you time it very carefully! :laugh:


Linq只是一种查询语言。您只需在一组上面定义查询。我们不要考虑延迟执行或并行性的可能性,但这里的关键是集合。 Linq不在,我们谈论Linq实体,对象,sql,odb ......还有很多其他东西,因为有人提供了linq接口(提供者 [ ^ ])。



如果写这篇文章的话所以它实现了比foreach更快的搜索/过滤,如果不是,你将拥有它。



如果有一个数据库引擎,linq查询是有针对性的,数据库模式设计得很好,索引在那里,答案可能是肯定的。如果你查询一个数组,那么答案将是no。
Linq is just a query language. You simply define your query above a set of somethings. Let's not think about deferred execution or possibilities of parallelism, but the key here is that set of something. Linq is not on it's on, we speak about Linq to entities, objects, sql, odb... and many other things for that somebody provides linq interface (provider[^]).

If this porvider written so it implements a faster search/filter than foreach, than you will have it, if not, than not.

If there is a database engine to which the linq query is directed, and the database schema is well designed, and the indexes are there, than the answer is probably yes. If you query an array, than the answer will be no.


一如既往的问题:哪个更快? - >根据您的具体情况进行测量。

一般情况下:您可以假设每个抽象级别在执行速度方面获得的优化代码较少(因为抽象通常会产生通用代码而不是特定代码)。 br />


所以,再次:根据您的具体情况进行测量。



干杯

Andi
As always with the kind of question: "Which is faster?" --> measure it for your particular case.
In general: you can assume with each level of abstraction to get less optimal code in terms of execution speed (since abstraction usually produces generic code instead of specific code).

So, again: measure it for your particular situation.

Cheers
Andi


这篇关于LINQ版本比foreach版本更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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