Recently I was asked a simple code to improve the performance, and at first I couldn’t see it but as you know your brain starts working when you aren’t thinking of the problem anymore. (It happen to me when I was driving)

Anyway the problem on hand was there is a for loop and when there are 10000 clients it performs bad.
Something along the line of.

So I went and wrote some code to see the perf using Linq and at the end using a Dictionary rather, since a dictionary is way faster in lookup, O(1) in this case.

Here is the code using a List and Linq together

and if we changed the datatype to Dictionary this was the code used

Here is the end result of using it on an i7 8G of ram machine.


And the results show that using an index as a dictionary is definitely faster, but one also has to look at the number of data.
Where there are only few data entry using linq is quite insignificant in performance but where there are millions of data, it really shows that it slows down.

Here is the rest of the code.