1

There are 3 threads. Each of them works (reads, writes) with its own set of dictionary keys. So keys are mutually exclusive for different threads. There are also multiple threads which only read data.

Which of these two approaches is more efficient in terms of speed:

  1. Create a single dictionary (of type ConcurrentDictionary)
  2. Create a separate dictionary (of type ConcurrentDictionary) for each of those 3 threads.

At first glance, the 2nd approach is more efficient since there are no writer contentions. Are there any pitfalls here? If difference between two approaches is insignificant then I would go with the 1st approach.

Andrei Sedoi
  • 1,534
  • 1
  • 15
  • 28
  • 2
    What do the other threads reading data need to do? Have you tried both and measured the difference? Have you got concrete performance targets already? – Jon Skeet Jun 28 '11 at 11:02
  • Other threads iterate over dictionaries reading values. I haven't tried neither one yet. I just want to know whether there are benefits of having disjoint sets of keys for different threads when working with a single `ConcurrentDictionary` dictionary. Obviously if the entire dictionary is locked when writing to it then there are no benefits. AFAIK `ConcurrentDictionary` doesn't work this way. – Andrei Sedoi Jun 28 '11 at 11:24

1 Answers1

0

The 2nd approach is more efficient. In any case it's not a good idea to have a shared state.

Andrei Sedoi
  • 1,534
  • 1
  • 15
  • 28