by skeet via Jon Skeet: Coding Blog on 1/29/2009 11:08:02 PM
As promised earlier in the week, here are the results of benchmarking for and foreach.
for
foreach
For each of int and double, I created an array and a List<T>, filled it with random data (the same for the list as the array) and ran each of the following ways of summing the collection:
int
double
List<T>
IEnumerable<T>
I won't show the complete code in this post, but it's you can download it and then build it against the benchmarking framework. Here's a taste of what it looks like - the code for a list instead of an array, and double instead of int is pretty similar:
(I don't normally format code quite like that, and wouldn't even use a lambda for that sort of code - but it shows everything quite compactly for the sake of blogging.)
Before I present the results, a little explanation:
int[]
double[]
List<int>
Happy with that? Here are the results...
I found the results interesting to say the least. Observations:
If anyone fancies running this on their own box and letting me know if they get very different results, that would be really interesting. Likewise let me know if you want me to add any more tests into the mix.
Original Post: For vs Foreach on arrays and lists
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.