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 Query

More Query from same tag