LINQ versus looping–Performance
Looping and Iterating is bad for performance right? So – a loop like the one below should be bad.
for (int i = 0; i <= iterations; i++) { foreach (int j in data) { if (j < 100) searchResults.Add(j); } }
Try LINQ?
The first alternative that comes to mind is to use LINQ. I can replace all the lines of code above with a single LINQ line :
from num in data where num < 100 select num
In fact, LINQ is so popular now, that this is exactly what everyone (including myself) does. So – what’s the problem with doing a simple select like the one above?
Performance
LINQ was designed to be fast, so it came as a bit of a surprise to me when I ran a simple comparison (between a regular loop above and a LINQ query which does the same thing) – and found LINQ to be slower.
The LINQ query took twice as long – for a list of 3000 integers. Something was wrong. LINQ was supposed to outperform the loop. Next, I increased the size of the test array to 40000 integers. This time, LINQ was clearly faster – by a factor of 5.
RESULTS for 3000 items in the test array (Loop is faster than LINQ)
RESULTS for 40000 items in the test array (LINQ kicks LOOP’s butt by a factor of 5)
RESULTS for 200000 items in the test array (LINQ kicks LOOP’s butt by a factor of 25)
SUMMARY
Initially, it seemed that LINQ was not performing the way it was supposed to. And for small sized collections, it does seem to offer little improvement over regular looping. However, as the collection size grew, LINQ stepped up to the plate. The total time for the LINQ query stayed fairly constant. The same cannot be said for our for loop – which had a lot of trouble with larger sized collections. So – all is right with the world – regular looping and iterating is still slow as heck – and LINQ offers a nice, speedier alternative.
This article is very misleading, you’re not doing anything with the LINQ result so of course the performance is better… it’s not being executed! Do something with the LINQ result (get a count of the matches for example) and you will see the performance is worse than the foreach loop. Your also sticking all the foreach loop results into a single array while not doing the same with LINQ.
Thanks for the feedback. I tried it with the .count on the Linq Results. LINQ still seems to outperform the for loop significantly (see attached source code).
Hi there, your still only executing the LINQ query once (the LINQ results finishes with 100 items vs 10100).
Change searchResults in the LINQ query to be an integer array and AddRange the LINQ select to it. Both arrays will now match but the LINQ will be clearly slower.