954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why is Linq faster than normal .net framework methods?

Well first of all, I want to know if it is true, is Linq faster than default Collections methods like, contains(), size() etc...

for example if I load a 3MB text list into a List to find a specific string, will it be faster to use Linq or normal methos like foreach + Equals()?

RicardoE
Junior Poster in Training
73 posts since Jan 2010
Reputation Points: 21
Solved Threads: 7
 

To test the speed of foreach vs LINQ I created ten million GUID then randomly picked one to search for. The two methods tested where:

foreach (String s in myList) {
    if (s.Equals(toFind)) {
        found = s;
        break;
    }
}

var found = myList.Find(n => n.Equals(toFind));


There was no difference in speed. Decompliling into assembly shows that the LINQ method makes several calls into routines in the .Net framework that the foreach method does not make. I'd assume those are the routines that handle the enumeration through the list for LINQ.

Just FYI ten million GUID is 360 megabytes of data.

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: