Search code examples
c#-4.0task-parallel-libraryconcurrencyconcurrent-collections

Parallel foreach ConcurrentDictionary<string, string> add


I have entries like in a phone book: name + address. The source is on a web site, the count is over 1K records.

Question is:

How do i use/implement ConcurrentDictionary with ParallelForeach?

I might as well ask will it better perform:

ConcurrentDictionary & ParallelForeach

vs

Dictionary & foreach

As the name is not allowed to have duplicates being the key, and i think i understood correctly that ConcurrentDictionary has its own built-in function to add(TryAdd) only if key does not exists. so the issue of not allowing adding duplicated keys already taken cared of, so from that point i could clearly see the balance is turning towards ConcurrentDictionary rather than standard-sequential Dictionary

So how do I add name & address from any given data source and load it via Parallelforeach into a ConcurrentDictionary


Solution

  • the count is over 1K records.

    How much over 1K? Because 1K records would be added in the blink of an eye, without any need for parallelization.

    Additionally, if you're fetching the data over the network, that cost will vastly dwarf the cost of adding to a dictionary. So unless you can parallelize fetching the data, there's going to be no point in making the code more complicated to add the data to the dictionary in parallel.