Come menzionato da Jim Mischel, è impossibile eseguire una singola ricerca per modificare il valore dell'articolo del dizionario. ConcurrentDictionary.AddOrUpdate
metodo di fare più di un'operazione di ricerca (fonti riflesse):
public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
{
TValue local2;
if (key == null)
{
throw new ArgumentNullException("key");
}
if (updateValueFactory == null)
{
throw new ArgumentNullException("updateValueFactory");
}
do
{
TValue local3;
while (this.TryGetValue(key, out local3))
{
TValue newValue = updateValueFactory(key, local3);
if (this.TryUpdate(key, newValue, local3))
{
return newValue;
}
}
}
while (!this.TryAddInternal(key, addValue, false, true, out local2));
return local2;
}
Ho fatto test delle prestazioni con il dizionario simultaneo e semplice ditcionary:
estensione AddOrUpdate per IDictionary:
public static class DictionaryExtensions
{
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue initValue, Func<TKey, TValue, TValue> updateFunc)
{
TValue value;
value = dict.TryGetValue(key, out value) ? updateFunc(key, value) : initValue;
dict[key] = value;
}
}
Test:
static void Main(string[] args)
{
const int dictLength = 100000;
const int testCount = 1000000;
var cdict = new ConcurrentDictionary<string, int>(GetRandomData(dictLength));
var dict = GetRandomData(dictLength).ToDictionary(x => x.Key, x => x.Value);
var stopwatch = new Stopwatch();
stopwatch.Start();
foreach (var pair in GetRandomData(testCount))
cdict.AddOrUpdate(pair.Key, 1, (x, y) => y+1);
stopwatch.Stop();
Console.WriteLine("Concurrent dictionary: {0}", stopwatch.ElapsedMilliseconds);
stopwatch.Reset();
stopwatch.Start();
foreach (var pair in GetRandomData(testCount))
dict.AddOrUpdate(pair.Key, 1, (x, y) => y+1);
stopwatch.Stop();
Console.WriteLine("Dictionary: {0}", stopwatch.ElapsedMilliseconds);
Console.ReadLine();
}
static IEnumerable<KeyValuePair<string, int>> GetRandomData(int count)
{
const int constSeed = 100;
var randGenerator = new Random(constSeed);
return Enumerable.Range(0, count).Select((x, ind) => new KeyValuePair<string, int>(randGenerator.Next().ToString() + "_" + ind, randGenerator.Next()));
}
Risultati di analisi su mio ambiente (ms):
ConcurrentDictionary: 2504
Dictionary: 1351
Quindi stai cercando di eliminare una delle ricerche in uno scenario di aggiunta o aggiornamento? – mydogisbox
Il dizionario concorrente sembra abbastanza performante in molti casi, hai controllato se fornisce prestazioni sufficienti per il tuo scenario? – Alex
puoi ordinare i valori-chiave? Immagino che la maggior parte sarà O (n log n) quindi potresti dover testare per ottenere le migliori prestazioni – Carsten