Which LINQ query is more efficient?

I have a huge IEnumerable (suppose it's the name myItems), which method is more efficient?

Solution 1: filter it first and then run.

Array.ForEach(myItems.Where(FILTER-IT-HERE).ToArray(),MY-ACTION); 

Solution 2: RETURN TO MY ACTION if the item is not up to mustard.

 Array.ForEach(myItems.ToArray(),MY-ACTION-WITH-FILTER); 

Is one of them always better than the other? Or any other good suggestions? Thanks in advance.

+4
source share
7 answers

Well, both times, you convert to an array, which may not be as efficient if IEnumerable is very large (as you said). You can create a general extension method for IEnumerable, for example:

 public static void ForEach<T>(this IEnumerable<T> current, Action<T> action) { foreach (var i in current) { action(i); } } 

and then you can do this:

 IEnumerable<int> ints = new List<int>(); ints.Where(i => i == 5).ForEach(i => Console.WriteLine(i)); 
0
source

Did you take any measurements? Since we cannot measure the runtime of My-Action, you can only do this. Measure and decide.

+1
source

Sometimes you need to create tests because similar actions can lead to radical and unexpected results.

You are not saying that your data source, so Iโ€™m going to assume that it can be data on the SQL server, in which case server-side filtering will probably always be the best approach since you minimized the amount of translation data. Accessing memory is always faster than transferring data from disk to memory, so when you can transfer fewer records, you are likely to get better performance.

+1
source

All things being equal, a call to ToArray() will first produce a larger result than when it was last called. Although, as pointed out by others before me,

  • Why use ToArray() and Array.ForEach() in general?
  • We do not know that everything else is actually the same, because you do not disclose the details of the implementation of your filter and action.
+1
source

If performance is a concern, I donโ€™t understand why you are trying to build the whole array in the first place. Why not just that?

 foreach (var item in myItems.Where(FILTER-IT-HERE)) MY-ACTION; 

Or:

 foreach (var item in myItems) MY-ACTION-WITH-FILTER; 

I ask because, while the others are right that you cannot know without testing, I would not expect that there will be a big difference between the two options. On the other hand, I will expect a difference between creating / filling an array (apparently for no reason) and not creating an array.

0
source

LINQ's idea is to work on enumerable collections, so the best LINQ query is one where you don't use Array.ForEach() and .ToArray() .

0
source

I would say that this belongs to the category of premature optimization. If, after installing the tests, you find that the code is too slow, you can always try each approach and choose the result that works best for you.

Since we donโ€™t know how IEnumerable<> , itโ€™s hard to say which approach will work best. We also donโ€™t know how many elements will remain after applying your predicate - and we donโ€™t know whether action or iteration steps will be the dominant factor in the execution of your code. The only way to know for sure is to try in both directions, profile the results and choose the best one.

Performance aside, I would choose the most understandable version, which (for me) is the first filter, and then apply the projection to the result.

-1
source

Source: https://habr.com/ru/post/1308957/


All Articles