C# .Net 中 LINQ 和 Lambda 表达式的效率和性能如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1182922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:14:34  来源:igfitidea点击:

What is the Efficiency and Performance of LINQ and Lambda Expression in .Net?

c#.netlinq.net-3.5lambda

提问by David.Chu.ca

I have used .Net 3.5 and VS 2008 for more than a month. Like most .Net developers, I have evolved from years experience in .Net 1.0 & 2.0 and VS 2005. Just recently, I discovered the simplicity and power of LINQ and Lambda Expressions, as in my recent questions such as Find an item in list by LINQ, Convert or map a class instance to a list of another one by using Lambda or LINQ, and Convert or map a list of class to another list of class by using Lambda or LINQ.

我已经使用 .Net 3.5 和 VS 2008 一个多月了。像大多数 .Net 开发人员一样,我是从 .Net 1.0 & 2.0 和 VS 2005 的多年经验中发展而来的。就在最近,我发现了 LINQ 和 Lambda 表达式的简单性和强大功能,就像在我最近的问题中一样,例如Find an item in list by LINQ使用 Lambda 或 LINQ类实例转换或映射到另一个类实例的列表,以及使用 Lambda 或 LINQ将类列表转换或映射到另一个类列表

I admit that Lambda and LINQ are much simpler and easy to read and they seem very powerful. Behind the scenes, the .Net compiler must generate lots of code to achieve those functions. Therefore I am little bit hesitant to switch to the new syntax since I already know the "old" way to achieve the same results.

我承认 Lambda 和 LINQ 更简单易读,而且它们看起来非常强大。在幕后,.Net 编译器必须生成大量代码来实现这些功能。因此,我对切换到新语法有点犹豫,因为我已经知道实现相同结果的“旧”方法。

My question is the about the efficiency and performance of Lambda and LINQ. Maybe Lambda expressions are mostly in-line functions, in that case I guess Lambda should be okay. How about LINQ?

我的问题是关于 Lambda 和 LINQ 的效率和性能。也许 Lambda 表达式主要是内联函数,在这种情况下,我想 Lambda 应该没问题。LINQ呢?

Let's limit the discussion to LINQ-to-Objects LINQ-to-SQL (LINQ-to-SQL). Any comments, comparison and experiences?

让我们将讨论限制在 LINQ-to-Objects LINQ-to-SQL (LINQ-to-SQL) 上。有什么评论、比较和经验吗?

采纳答案by Lasse V. Karlsen

There's no one single answer that will suffice here.

这里没有一个单一的答案就足够了。

LINQ has many uses, and many implementations, and thus many implications to the efficiency of your code.

LINQ 有很多用途和实现,因此对代码的效率有很多影响。

As with every piece of technology at our fingertips, LINQ can and will be abused and misused alike, and the ability to distinguish between that, and proper usage, is only dependent on one thing: knowledge.

与我们触手可及的每一项技术一样,LINQ 可以也将会被滥用和误用,而区分这些和正确使用的能力仅取决于一件事:知识。

So the best advice I can give you is to go and read up on how LINQ is really implemented.

所以我能给你的最好建议是去阅读 LINQ 是如何真正实现的。

Things you should check into are:

您应该检查的事项是:

And as always, when looking at efficiency questions, the only safe approach is just to measure. Create a piece of code using LINQ that does a single, know, thing, and create an alternative, then measure both, and try to improve. Guessing and assuming will only lead to bad results.

与往常一样,在查看效率问题时,唯一安全的方法就是测量。使用 LINQ 创建一段代码,它做一个单一的、知道的事情,并创建一个替代方案,然后衡量两者,并尝试改进。猜测和假设只会导致不好的结果。

回答by ecspot

In some cases LINQ is just as fast if not faster than other methods, but in other cases it can be slower. We work on a project that we converted to linq and the data lookup is faster but the merging of data between two tables is much slower. There is a little overhead, but in most cases I don't see the speed difference having much effect on your program.

在某些情况下,LINQ 的速度与其他方法一样快,但在其他情况下,它可能会更慢。我们正在处理一个转换为 linq 的项目,数据查找速度更快,但两个表之间的数据合并要慢得多。有一点开销,但在大多数情况下,我认为速度差异对您的程序没有太大影响。

回答by Michael Morton

For LINQ queries, with the 'new syntax', the IL (code) generated, is fundamentally no different than calling the extension methods provided by Enumerable and Queryable directly.

对于 LINQ 查询,使用“新语法”,生成的 IL(代码)与直接调用 Enumerable 和 Queryable 提供的扩展方法从根本上没有什么不同。

回答by sbreitzke

Dont optimize prematurely. Use Linq and the new extension methods liberally if they improve readability and profile your application afterwards.

不要过早优化。如果 Linq 和新的扩展方法可以提高可读性并在之后分析您的应用程序,请自由使用它们。

Most times the difference between Linq and using plain for loops is not relevant at all. The improved maintainability of your code should be worth a few ms. Linq can be slower because it works on enumerators which are implemented as state machines. So plain for(...) loops will be faster.

大多数情况下,Linq 和使用普通 for 循环之间的区别根本不相关。改进的代码可维护性应该值得花费几毫秒。Linq 可能较慢,因为它适用于作为状态机实现的枚举器。所以普通的 for(...) 循环会更快。

I would recommend following Lasse V. Karlsens advice and append http://www.davesquared.net/2009/07/enumerables-linq-and-speed.htmlto his link list.

我建议遵循 Lasse V. Karlsens 的建议并将http://www.davesquared.net/2009/07/enumerables-linq-and-speed.html附加到他的链接列表中。

回答by George Mauer

Technically the fastest way is to control all the minutia yourself. Here are some performance tests. Notice that the foreach keyword and the ForEach LINQ construct are identically far far slower than just using for and writing procedural code.

从技术上讲,最快的方法是自己控制所有细节。 下面是一些性能测试。请注意,foreach 关键字和 ForEach LINQ 构造同样比仅使用 for 和编写过程代码慢得多。

However, the compiler can and will be improved and you can always profile your code and optimize any problematic areas. It is generally recommended to use the more expressive features that make code easier to read unless you really need the extra nanoseconds.

但是,编译器可以并且将会得到改进,您可以随时分析代码并优化任何有问题的区域。通常建议使用更具表现力的功能,使代码更易于阅读,除非您确实需要额外的纳秒。

回答by Balaji Umapathy

There is no performance difference between LINQ queries and Lambda expressions.

LINQ 查询和 Lambda 表达式之间没有性能差异。

You should completely understand how LINQ feature(both Lambda, LINQ queries) works in .Net before you are looking into performance issues.

在研究性能问题之前,您应该完全了解 LINQ 功能(包括 Lambda 和 LINQ 查询)在 .Net 中的工作原理。

Basically you can work with any one of both LINQ queries and Lambda expressions..

基本上,您可以使用 LINQ 查询和 Lambda 表达式中的任何一种。

LINQ Queries

LINQ 查询

  1. It is high level readable query.

  2. It is converted into equalent Lambda expressions and Lambda expressions added as nodes into an expression tree. Expression tree which makes structure of lambda expressions. This is done by compiler.

  3. Query provider looks into expressions(added as nodes in expression tree) and produces equalent SQL query operators thus equalent sql query formed during runtime.

  4. Return Type : Result set (IEnumerable).

  1. 它是高级可读查询。

  2. 它被转换为相等的 Lambda 表达式,并将 Lambda 表达式作为节点添加到表达式树中。构成 lambda 表达式结构的表达式树。这是由编译器完成的。

  3. 查询提供程序查看表达式(添加为表达式树中的节点)并生成相等的 SQL 查询运算符,从而在运行时形成相等的 sql 查询。

  4. 返回类型:结果集(IEnumerable)。

Lambda Expressions

Lambda 表达式

  1. It is a set of expressions/statements and creates delegate / expression tree. It can be passed to a function as an argument.

  2. It supports all the LINQ methods like LINQ queries. (Where,Select,Count,Sum,etc)

  3. An expression tree formed which makes structure of lambda expressions. This is done by compiler.

  4. Query provider looks into expressions(expression tree) and produces equalent SQL query during runtime.

  5. Return Type : Delagate / Expression Tree

  1. 它是一组表达式/语句并创建委托/表达式树。它可以作为参数传递给函数。

  2. 它支持所有 LINQ 方法,如 LINQ 查询。(哪里,选择,计数,总和等)

  3. 形成的表达式树构成了 lambda 表达式的结构。这是由编译器完成的。

  4. 查询提供程序查看表达式(表达式树)并在运行时生成相等的 SQL 查询。

  5. 返回类型:延迟/表达式树

Which is Best?

哪个最好?

You can understand the LINQ (Queries,Lambda) If you look into the above points.

如果您查看以上几点,您就可以理解 LINQ (Queries,Lambda)。

Advantage of LINQ query - It is readable.

LINQ 查询的优点 - 它是可读的。

Advantage of Lambda

Lambda 的优势

  1. Lambda will have an advantage since it creates a delegate and by using the delagte you can just pass the input paremeters and get the result for the different input parameters.You need not write different queries for different criteria as well.

  2. You can create dynamic query by using Lambda expressions and Expression trees.

  3. You can use Lambda expressions if you want to pass the result of statement(s) to a method as an argument.

  4. expressions are shorter.

  1. Lambda 将具有优势,因为它创建了一个委托,并且通过使用 delagte,您可以只传递输入参数并获得不同输入参数的结果。您也不需要为不同的条件编写不同的查询。

  2. 您可以使用 Lambda 表达式和表达式树创建动态查询。

  3. 如果要将语句的结果作为参数传递给方法,则可以使用 Lambda 表达式。

  4. 表达式更短。

So Lambda expression is the best for development over LINQ queries.

因此,Lambda 表达式最适合通过 LINQ 查询进行开发。