score:2

First, this sounds like a obvious case for the speed rant:

https://ericlippert.com/2012/12/17/performance-rant/

Second:

The best way is to keep this in the Database. You are not going to beat the speed of a DB Query with Indexes designed for quick matching, by transfering the data over the network twice (once to get it, once to return it) and doubling the search load (once to get all odd ones, once to update all the ones you just changed). My standing advice is to always keep as much work as possible on the DB side. Your client code will never be able to beat it.

Third:

If you do need to use client side processing:

Now a lot of my answer depend on details of the implementation, how the JiT and general Compiler optimsiations work, etc.

Foreach uses works on enumerators, not Collections. But if you feed a collection to foreaach, a Enumerator is implicitly created. Now enumerators do have two properties:

  1. If the collection changes, the Enumerator becomes invalid. Most people learn about them because they ran into this issue.
  2. It is a extra function call and set of checks for accessing a collection. So it will be a slowdown. How much is hard to say, as the Optimisations and JiT are pretty good.

So you propably want to use for loop instead.

If you could turn the Dictionary into a collection where the Primary Key is used as Index, it might be a bit faster. But hat has the danger of running into a lot of "dry spells" regarding data, so it depends a lot on your source data.


Related Articles