Skip to content

Performance of Loops on Collections

Interesting article from Patrick Smacchia (the NDepend guy) in which he runs some benchmarks on the cost of looping in different ways over different constructs and comes to the conclusion that:

“looping on array using for is 5 times cheaper than looping on List<T> using foreach (which I believe, is what we all do)”

Well worth a read, although I suspect that in my solutions the bottlenecks will remain the out-of-process calls, i.e. database access and web service calls. Still, a 5x gain is not to be sniffed at.

Patrick Smacchia – An easy and efficient way to improve .NET code performance.

Published inTech
Copyright © Ian Fraser Nelson 2023