Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
3.75/5 (3 votes)
See more:
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.
Posted
Updated 11-Jun-11 20:45pm
v3
Comments
Sergey Alexandrovich Kryukov 12-Jun-11 2:40am    
Did you ask yourself a simple question: improved (or degrades) performance -- compared to what?
--SA
RakeshMeena 12-Jun-11 2:43am    
My mistake. Edited the question.
Sergey Alexandrovich Kryukov 12-Jun-11 23:20pm    
Thank you. Answered.
--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
 
Share this answer
 
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.
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900