I need to iterate and count. What is fastest or preferred: ToArray() or ToList()? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-19 12:29:11

问题


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?


回答1:


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.




回答2:


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.




回答3:


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<>?



来源:https://stackoverflow.com/questions/1826658/i-need-to-iterate-and-count-what-is-fastest-or-preferred-toarray-or-tolist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!