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)

linq_versus_loop

RESULTS for 40000 items in the test array (LINQ kicks LOOP’s butt by a factor of 5)

linq_versus_loop_40000_items

RESULTS for 200000 items in the test array (LINQ kicks LOOP’s butt by a factor of 25)

linq_versus_loop_200000_items

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.

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.