ArticleDirectory
Let's start from the basics and repeat some information you already know. One of the most important aspects in the optimization of LINQ performance is the delay of execution. This means that when you declare a variable and assign it a query string, the query string is not executed immediately.
// Query is not executed.
VaR query = from itemInStorage select item;
Variable query now stores commands, and the query execution is delayed until you get data from the variable query request. This usually happens in the following situations: foreach loop, or when you call a clustering function like Min, Max, and average, or when you use the tolist or toarray method to cache the query results.
// Foreach loop.Foreach(VAR itemInQuery) console. writeline (item );// Count function.IntTotal = query. Count ();// Toarray method.VaR cachedquery = query. toarray ();
Now let's take a look at what happened behind the scenes. Is there any compiler-level optimization during query execution? The answer is yes. However, there is a trap. From now on, we will only discuss how to useProgramQuery ienumerable and ienumerable <t> sets. For other LINQ providers, including LINQ to SQL and LINQ to XML, different optimization rules may be applied.
Note:It is often believed that the execution of the first query takes a longer time due to delay. However, in LINQ to objects, there is no difference between the first execution and every subsequent execution. The rules of other LINQ providers may be different (for example, some of them may be cached), but you need to refer to detailed documents of specific providers.
In the following cases, the LINQ to objects query is optimized:
- Some method calls will be optimized if the data source implements a necessary interface. The following table lists these optimizations.
| LINQ Method |
Optimization |
| Cast |
If the data source has implemented the ienumerable interface for the given type T <t>, the data sequence is directly returned without conversion. |
| Contains |
If the data source implements the interface icollection or icollection <t>, the corresponding method of the interface will be used. |
| Count |
If the data source implements the interface ilist or ilist <t>, the interface's count method and indexer will be used. |
Elementat Elementatordefault First Firstordefault Last Lastordefault Single Singleordefault |
If one or more consecutive select operations are followed by one or more consecutive where operations, the query creates only one ienumerable or ienumerable <t> object instead of an intermediate object. |
Consider the following query:
VaR query = from itemInStoragewhere item. Category ="Food"Where item. Price <100 select item;
Here, the query creates only one ienumerable object.
- If you query an array or list, the API ienumerable or ienumerable <t> does not use the enumerator in the foreach loop. On the contrary, a simple for loop of an array or list is created before use, and the elements are directly accessed.
In addition, the where operator implements a simple if statement, so there is no intermediate enumerator.
Again, other LINQ providers may have their own performance optimization rules. However, the above rules should give you some comments on how to use LINQ to objects.
Original article: Alexander Rusina does the "LINQ to objects" provider have built-in performance optimization?
[To] http://www.cnblogs.com/tianfan/archive/2010/03/03/does-the-linq-to-objects-provider-have-built-in-performance-optimization.html
* It is also prepared for translation, just as tianfan has done. So go directly here. Hard work