由 for 和 foreach 的性能问题想到的...

966 阅读1分钟
原文链接: github.com

Today our customer report a good point to improve the performance in our product. It is about the loop in .NET. In the test, they found the for loop is has better performance than foreach. I know this one for a long time. But when I verified it, I found another case, the loop doesn't affect the performance too much, but really affected by the type of list you are using. See the result below.

Loop with List

preview-list

Loop with Array

preview-array

Loop with ArrayList

preview-arraylist

Loop with Collection

preview-collection

If I didn't do this test, I cannot tell which is better. I used to use Collection<T> a lot; but next time, I guess I need to be more careful to use the list type from now on. The result is: Use Array > List<T> > ArrayList > Collection<T>.

PS. this source code can be compiled by .NET 3.5 Client profiler ~ .NET 4.5.2. So some statement is not fashion.