.Net 中 LINQ 和 Lambda 表达式的效率和性能如何? [英] What is the Efficiency and Performance of LINQ and Lambda Expression in .Net?

查看:35
本文介绍了.Net 中 LINQ 和 Lambda 表达式的效率和性能如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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 的效率和性能.也许 Lambda 表达式主要是内联函数,在这种情况下,我想 Lambda 应该没问题.LINQ呢?

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?

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

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

推荐答案

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

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

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

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 以及它如何使用现有集合类型的方法和扩展方法

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

    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.

    这篇关于.Net 中 LINQ 和 Lambda 表达式的效率和性能如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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