I'm working with this method for updating dictionary objects and wondered what its performance really is - whether it is constant time or there will be some dependencies on the number of kvp (key value pairs) stored? Does anyone have any idea of the performance for updating a dictionary with this method.
If you're talking about ConcurrentDictionary.AddOrUpdate()
, performance will depend on a couple of factors.
The dictionary is implemented as a generic hashtable. As such, average insertion in a single thread scenario will run in constant time, with the constant being dependent on the load factor and the quality of the function used to hash the keys.
In the multi-threaded case, insertion time may become heavily linked to the probability of a write clash, i.e. the likelihood that 2 threads try to write the dictionary at the same time. The latter is proportional to the number of threads using the data structure and the frequency of write operations.