Efficiency ToList ()

Many developers with whom I work feel more comfortable working with List , unlike IEnumerable (for example). I am wondering if there is a performance impact for ToList() overuse. For example, or, using ToList() , after ordering, to return the list again, i.e.

 private void ListThinger(List<T> input) { input = input.OrderBy(s => s.Thing).ToList(); foreach(var thing in input) { // do things } } 

My question is:

  • How effective is the ToList() method? Will he create a new list and how much memory will it do, assuming these are POCOs? Does this change if it is a value type and not a POCO?
  • Will the size of the list determine the effectiveness or the size of the list does not determine the value of ToList() ?
  • If the list is passed in IEnumerable and then ToList() is ToList() , will it just return the original object?

Ps I understand that a single use of ToList will not break our backs, but we are creating a highly competitive system that is currently connected to the processor, so I'm looking for small gains that will be added to the big improvement when scaling

+5
source share
2 answers

How effective is the ToList () method? Will he create a new list and how much memory will it do, assuming these are POCOs? Does this change if it is a value type and not a POCO?

The ToList() method implements this collection by creating a new list and filling it with elements of this collection. Linq.ToList() implementation :

 public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source) { if (source == null) throw Error.ArgumentNull("source"); return new List<TSource>(source); } 

By doing so, you do not gain the strength of a defensive execution if necessary


Will the size of the list determine the effectiveness or the size of the list does not determine the value of ToList ()?

As he calls the List copy constructor, and he creates a new list, then he will work on each of the elements. Therefore, it will work in O(n) - this means that the size of the list matters. MSDN documentation about copy constructor work:

Initializes a new instance of the List class, which contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.

As mentioned in the comments by @Jason's comment, Copy Constructor is smart and efficient, but does it when it is not needed, another O(n) operation that should not happen


If the list is passed in IEnumerable and then ToList () is called on it, will it just return the original object?

No. It will create a new list as shown above.


As for your sample code:

 input = input.OrderBy(s => s.Thing).ToList(); foreach(var thing in input) { // do things } 

As you get a materialized list (and not IQueriable / IEnumerable , which can be run in differentiated execution), adding ToList after adding does not do you any good.

You can look here, it can also help: When to use LINQ.ToList () or .ToArray ()

+3
source
  • Yes creates a new list. It is difficult to accurately measure memory usage, but most likely it will be the size of the class + (number of elements of the system word * number of elements). I recommend the memory profiler.
  • The algorithmic efficiency of operations, of course, will affect the number of elements.
  • Yes, you get a new list every time. The links inside are not duplicated, but primitives.

Try it yourself:

 var list = new List<int>(); bool areListsTheSame = list == ((IEnumerable<int>)list).ToList(); 
+1
source

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


All Articles