LINQ是否会提高性能 [英] Does LINQ improves performance

查看:128
本文介绍了LINQ是否会提高性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望您对LINQ是提供更好的性能(相比于诸如foreach之类的循环结构)还是只是框架的语法糖功能的观点.我已经使用LINQ已有相当一天了,我的前辈对这个话题有不同的看法.
如果可以提供一些见解.

Want your views on whether LINQ provides better performance (compared to say loop constructs like foreach) or it''s just a syntactic sugar feature of the framework. I''ve been using LINQ from quite some day and got conflicting views from my seniors on the topic.
If you can provide some insights.

推荐答案

您可以使用类System.Diagnostics.Stopwatch自己轻松比较此类简单情况的性能,该类提供了非常好的准确性.我的预测:如果正确编程,则根本不会发现任何显着差异.让我们检查一下吧!

—SA
You can easily compare performance for such simple cases yourself using the class System.Diagnostics.Stopwatch, which provides very good accuracy. My prediction: if you program you loops correctly, you won''t find any significant difference if any at all. Let''s check it up!

—SA


Rakesh,

这取决于.例如:对于简单的for-each循环,不需要使用LINQ,相反,它可以提供更多开销.但是在要添加其他过滤和分组的位置使用for-each时,它会很慢,因此在这种情况下使用LINQ查询会更好.

那是关于Linq to Objects.考虑到要使用Linq to Sql的情况,我想推荐有关Linq to SQL性能的5部分系列文章:http://blogs.msdn.com/b/ricom/archive/2007/06/22/dlinq-linq -to-sql-performance-part-1.aspx [
Hi Rakesh,

It depends. For ex: for a simple for-each loop, using LINQ is not necessary, instead it can give more overhead. But using for-each where you want to add additional filtering and grouping, then it will be slow, it would be better to use a LINQ query in such conditions.

That was about Linq to Objects. Considering the case where you want to use Linq to Sql, I would like to recommend this 5-part series article regarding the performance of Linq to SQL : http://blogs.msdn.com/b/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx[^].

About Linq to XML, I''m not sure about the performance but its very easy to create XmlElement compared to the functionality provided by XmlDocument class. Its also easy to traverse and filter through the XML, also it has support to XPath expression using the XPathNavigator.
:)


这篇关于LINQ是否会提高性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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