using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
namespace R601EjemploConcurrentDictionary
{
class Programa
{
const string Elemento = "Elemento diccionario";
public static string ElementoActual;
private const int NUMERO_OPERACIONES = 1000000;
static void Main(string[] args)
{
var diccionarioConcurrente = new ConcurrentDictionary<int, String>();
var diccionario = new Dictionary<int, string>();
var cronometro = new Stopwatch();
cronometro.Start();
for (int i = 0; i < NUMERO_OPERACIONES; i++)
{
lock (diccionario)
{
diccionario[i] = Elemento;
}
}
cronometro.Stop();
Console.WriteLine("Tiempo de escritura sobre un objeto Dictionary con bloqueo: " + cronometro.Elapsed.Milliseconds.ToString());
cronometro.Restart();
for (int i = 0; i < NUMERO_OPERACIONES; i++)
{
diccionarioConcurrente[i] = Elemento;
}
cronometro.Stop();
Console.WriteLine("Tiempo de escritura sobre un objeto ConcurrentDictionary: " + cronometro.Elapsed.Milliseconds.ToString());
cronometro.Restart();
for (int i = 0; i < NUMERO_OPERACIONES; i++)
{
lock (diccionario)
{
ElementoActual = diccionario[i];
}
}
cronometro.Stop();
Console.WriteLine("Tiempo de lectura sobre un objeto Dictionary con bloqueo: " + cronometro.Elapsed.Milliseconds.ToString());
cronometro.Restart();
for (int i = 0; i < NUMERO_OPERACIONES; i++)
{
ElementoActual = diccionarioConcurrente[i];
}
cronometro.Stop();
Console.WriteLine("Tiempo de lectura sobre un objeto ConcurrentDictionary: " + cronometro.Elapsed.Milliseconds.ToString());
Console.ReadLine();
}
}
}