6

Collection Performance: How Do You LINQ? – dotNetTips.com

 2 years ago
source link: https://dotnettips.wordpress.com/2021/12/15/collection-performance-how-do-you-linq/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Collection Performance: How Do You LINQ?

Way, way back in 2007, the Language-Integrated Query (LINQ) was introduced as part of .NET 3.5. Since LINQ was new, I spent some time learning how to create the most performant queries, especially for Entity Framework. I learned back then how the query is written can affect performance. In this article, I will show the most performant way to write queries for a collection of objects.

The LINQ API & LAMBDA’S

In the beginning of LINQ API, this is how we wrote a simple where query:

var query = from person in PersonCollection
           where person.Age.TotalDays > 1000
            select person;

var result = query.ToList() // This runs the query

This same query can be written this way using a LAMBDA:

var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).ToList();

Which one do you think is faster?

LINQ: WHERE() CHARTThe speed of these two ways to write a simple where query is too close to call. Let’s see how the performance is for other common LINQ methods.

Any()

This LINQ Any() method returns a Boolean if there is even one item in the collection that meets the criteria. There are three ways to use Any().

// API
var query = from person in PersonCollection
           where person.Age.TotalDays > 1000
            select person;

var result = query.Any();    
// LAMBDA'S
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).Any();

OR

var result = PersonCollection.Any(p => p.Age.TotalDays > 1000);

Performance

LINQ: ANY() CHARTAs you can see, using Any() as the first method in a LAMBDA call is faster!

First() & FirstOrDefault()

The method First() returns the first item in the collection that meets the criteria while FirstOrDefault() will also return a default value. Below are the different ways to use these methods.

// API
var query = from person in PersonCollection
           where person.Age.TotalDays > 1000
            select person;

var result = query.First();

var result = query.FirstOrDefault();       
// LAMBDA'S
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).First();

var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).FirstOrDefault();

OR

var result = PersonCollection.First(p => p.Age.TotalDays > 1000);

var result = PersonCollection.FirstOrDefault(p => p.Age.TotalDays > 1000);

Performance

LINQ: FIRST() CHART

LINQ: FIRSTORDEFAULT() CHARTAs you can see, using First() or FirstOrDefault() as the first method a LAMBDA call is faster!

Last() & LastOrDefault()

The method Last() returns the last item in the collection that meets the criteria while LastOrDefualt() will also return a default value. Below are the different ways to use these methods.

// API
var query = from person in PersonCollection
           where person.Age.TotalDays > 1000
            select person;

var result = query.Last();

var result = query.LastOrDefault();
// LAMBDA'S
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).Last();

var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).LastOrDefault();

OR

var result = PersonCollection.Last(p => p.Age.TotalDays > 1000);

var result = PersonCollection.LastOrDefault(p => p.Age.TotalDays > 1000);

Performance

HOW DO YOU LINK-LAST-CHARTAs you can see, using Last() or LastOrDefault() as the first method in a LAMBDA call is much, much faster!

Summary

These benchmark results prove, without a doubt, that you should always use Any(), First(), FirstOrDefault, Last(), LastOrDefault or Where() at the beginning of a LAMBDA statement as shown below:

var result = PersonCollection.FirstOrDefault(p => p.Age.TotalDays > 1000);

I apologize if now you must go analyze your codebase for these methods and fix them if needed. It will increase the performance of your applications. If you have any comments, please make them below.

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.

The benchmark results in this article are only for your reference. Your results will be different, so make sure to benchmark your code using Benchmarkdotnet.

Tagged Code Performance

I live in San Diego, Ca USA View all posts by dotNetDave


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK