结果已经写在注释中
static void Main(string[] args)
{
var concurrentDictionary = new ConcurrentDictionary<int, string>();
var dictionary = new Dictionary<int, string>();
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
lock (dictionary)
{
dictionary[i] = Item;
}
}
sw.Stop();
Console.WriteLine("wrinting to dictionary with a lock: {0}", sw.Elapsed);
//wrinting to dictionary with a lock: 00:00:00.0633939
sw.Restart();
for (int i = 0; i < 1000000; i++)
{
concurrentDictionary[i] = Item;
}
sw.Stop();
Console.WriteLine("wrinting to a concurrent dictionary: {0}", sw.Elapsed);
//wrinting to a concurrent dictionary: 00:00:00.2889851
//对于写入操作并发词典要比普通带锁词典要慢
sw.Restart();
for (int i = 0; i < 1000000; i++)
{