1 つのスレッドで ConcurrentDictionary を使用したり、すべてが 1 つのスレッドで行われる場合はアクセスを同期しても意味がありません。もちろん、辞書は ConcrurrentDictionary を打ち負かします。
使用パターンとスレッド数に大きく依存します。これは、 ConcurrentDictionary がディクショナリとスレッド数の増加に伴うロックよりも優れていることを示すテストです。
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Run(1, 100000, 10);
Run(10, 100000, 10);
Run(100, 100000, 10);
Run(1000, 100000, 10);
Console.ReadKey();
}
static void Run(int threads, int count, int cycles)
{
Console.WriteLine("");
Console.WriteLine($"Threads: {threads}, items: {count}, cycles:{cycles}");
var semaphore = new SemaphoreSlim(0, threads);
var concurrentDictionary = new ConcurrentDictionary<int, string>();
for (int i = 0; i < threads; i++)
{
Thread t = new Thread(() => Run(concurrentDictionary, count, cycles, semaphore));
t.Start();
}
Thread.Sleep(1000);
var w = Stopwatch.StartNew();
semaphore.Release(threads);
for (int i = 0; i < threads; i++)
semaphore.Wait();
Console.WriteLine($"ConcurrentDictionary: {w.Elapsed}");
var dictionary = new Dictionary<int, string>();
for (int i = 0; i < threads; i++)
{
Thread t = new Thread(() => Run(dictionary, count, cycles, semaphore));
t.Start();
}
Thread.Sleep(1000);
w.Restart();
semaphore.Release(threads);
for (int i = 0; i < threads; i++)
semaphore.Wait();
Console.WriteLine($"Dictionary: {w.Elapsed}");
}
static void Run(ConcurrentDictionary<int, string> dic, int elements, int cycles, SemaphoreSlim semaphore)
{
semaphore.Wait();
try
{
for (int i = 0; i < cycles; i++)
for (int j = 0; j < elements; j++)
{
var x = dic.GetOrAdd(i, x => x.ToString());
}
}
finally
{
semaphore.Release();
}
}
static void Run(Dictionary<int, string> dic, int elements, int cycles, SemaphoreSlim semaphore)
{
semaphore.Wait();
try
{
for (int i = 0; i < cycles; i++)
for (int j = 0; j < elements; j++)
lock (dic)
{
if (!dic.TryGetValue(i, out string value))
dic[i] = i.ToString();
}
}
finally
{
semaphore.Release();
}
}
}
}
スレッド: 1、アイテム: 100000、サイクル: 10 ConcurrentDictionary: 00:00:00.0000499 辞書: 00:00:00.0000137
スレッド: 10、アイテム: 100000、サイクル: 10 ConcurrentDictionary: 00:00:00.0497413 Dictionary: 00:00:00.2638265
スレッド: 100、アイテム: 100000、サイクル: 10 ConcurrentDictionary: 00:00:00.2408781 Dictionary: 00:00:02.2257736
スレッド: 1000、アイテム: 100000、サイクル: 10 ConcurrentDictionary: 00:00:01.8196668 Dictionary: 00:00:25.5717232