Advertisement
Fhernd

Programa.cs

Mar 1st, 2018
1,386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5.  
  6. namespace R601EjemploConcurrentDictionary
  7. {
  8.     class Programa
  9.     {
  10.         const string Elemento = "Elemento diccionario";
  11.         public static string ElementoActual;
  12.         private const int NUMERO_OPERACIONES = 1000000;
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             var diccionarioConcurrente = new ConcurrentDictionary<int, String>();
  17.             var diccionario = new Dictionary<int, string>();
  18.  
  19.             var cronometro = new Stopwatch();
  20.  
  21.             cronometro.Start();
  22.  
  23.             for (int i = 0; i < NUMERO_OPERACIONES; i++)
  24.             {
  25.                 lock (diccionario)
  26.                 {
  27.                     diccionario[i] = Elemento;
  28.                 }
  29.             }
  30.  
  31.             cronometro.Stop();
  32.             Console.WriteLine("Tiempo de escritura sobre un objeto Dictionary con bloqueo: " + cronometro.Elapsed.Milliseconds.ToString());
  33.  
  34.  
  35.             cronometro.Restart();
  36.  
  37.             for (int i = 0; i < NUMERO_OPERACIONES; i++)
  38.             {
  39.                 diccionarioConcurrente[i] = Elemento;
  40.             }
  41.  
  42.             cronometro.Stop();
  43.             Console.WriteLine("Tiempo de escritura sobre un objeto ConcurrentDictionary: " + cronometro.Elapsed.Milliseconds.ToString());
  44.  
  45.  
  46.             cronometro.Restart();
  47.  
  48.             for (int i = 0; i < NUMERO_OPERACIONES; i++)
  49.             {
  50.                 lock (diccionario)
  51.                 {
  52.                     ElementoActual = diccionario[i];
  53.                 }
  54.             }
  55.  
  56.             cronometro.Stop();
  57.             Console.WriteLine("Tiempo de lectura sobre un objeto Dictionary con bloqueo: " + cronometro.Elapsed.Milliseconds.ToString());
  58.  
  59.  
  60.             cronometro.Restart();
  61.  
  62.             for (int i = 0; i < NUMERO_OPERACIONES; i++)
  63.             {
  64.                 ElementoActual = diccionarioConcurrente[i];
  65.             }
  66.  
  67.             cronometro.Stop();
  68.             Console.WriteLine("Tiempo de lectura sobre un objeto ConcurrentDictionary: " + cronometro.Elapsed.Milliseconds.ToString());
  69.  
  70.             Console.ReadLine();
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement