Question

Possible Duplicate:
Is it better to call ToList() or ToArray() in LINQ queries?

I have code like this:

void Foobar(string[] arr, Dictionary<string, string[]>)
{
   var t = arr.Intersect(dic.Keys).ToList(); // .or ToArray() ?
   foreach(var item in t)
   {
      ..
   }

   var j = t.Count; // also I need this
}

which method is preferred?

I could go without any but I need to know the size and I don't want to call Enumerable.Count<T>() - it seems do do more actions then Array<T>.Size or List<T>.Count. Am I right?

Was it helpful?

Solution

Actually, in the current MS implementation of Count(IEnumerable) there's a shortcut looking if the IEnumerable is an ICollection and calls Count on it. So the performance should be comparable for counting elements.

ToList and ToArray are a bit the same. If the IEnumerable is a ICollection, then the CopyTo method is called instead, which is a bit faster.

So, choose what makes your code the most readable, and benchmark for YOUR use case to have a definite answer.

Update: I did a naive benchmark.

Starting with an Array: var items = Enumerable.Range(1,1000).ToArray();

  • calling ToList() : 25ms / 10000
  • calling ToArray() : 23 ms / 10000

Starting with an IEnumerable: var items = Enumerable.Range(1,1000);

  • calling ToList() : 168ms / 10000
  • calling ToArray() : 171 ms / 10000

So basically you get comparable performance.

OTHER TIPS

If you are really concerned about performance, you should loop over the IEnumerable and count it as you go. This avoids having to create a new collection altogether, and the intersection only has to be iterated once:

void Foobar(string[] arr, Dictionary<string, string[]>)
{
   var t = arr.Intersect(dic.Keys);
   int count = 0;
   foreach(var item in t)
   {
      count++;
      ..
   }

   var j = count;
}

But like someone else said: this smells of micro-optimization. If performance really matters in this situation, at least do performance profiling to find out which method is really the fastest for you.

The difference is probably so small that it is worth just using the method that fits your needs better. Smells of micro-optimization.

And in this case, since all you are doing is enumerating the set and counting the set (both of which you can do with an IEnumerable), why not just leave it as an IEnumerable<>?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top