Advertisement
Fhernd

doubleVSdecimal.cs

Jul 24th, 2017
1,564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. class doubleVSdecimal
  2. {
  3.     static void Main()
  4.     {
  5.         Random aleatorio = new Random();
  6.  
  7.         decimal[] decimales = new decimal[100];
  8.         double[] doubles = new double[100];
  9.  
  10.         for (int i = 0; i < 100; i++)
  11.         {
  12.             decimales[i] = GenerarDecimal(aleatorio);
  13.             doubles[i] = aleatorio.NextDouble();
  14.         }
  15.  
  16.         System.Diagnostics.Stopwatch cronometro = new System.Diagnostics.Stopwatch();
  17.  
  18.         decimal acumuladorSumaDecimales = 0;
  19.         cronometro.Start();
  20.  
  21.         for (int i = 0; i < 100; i++)
  22.         {
  23.             acumuladorSumaDecimales += decimales[i];
  24.         }
  25.  
  26.         cronometro.Stop();
  27.  
  28.         Console.WriteLine("Tiempo transcurrido: {0}", cronometro.Elapsed.TotalMilliseconds);    // 0.0005 (puede variar)
  29.  
  30.         double acumuladorSumaDoubles = 0;
  31.  
  32.         cronometro.Reset();
  33.         cronometro.Start();
  34.  
  35.         for (int i = 0; i < 100; i++)
  36.         {
  37.             acumuladorSumaDoubles += doubles[i];
  38.         }
  39.  
  40.         cronometro.Stop();
  41.  
  42.         Console.WriteLine("Tiempo transcurrido: {0}", cronometro.Elapsed.TotalMilliseconds);    // 0.0005 (puede variar)
  43.  
  44.         Console.ReadKey();
  45.     }
  46.  
  47.     // Este método fue tomado desde [4]
  48.     public static decimal GenerarDecimal(Random r)
  49.     {
  50.         var a = (int)(uint.MaxValue * r.NextDouble());
  51.         var b = (int)(uint.MaxValue * r.NextDouble());
  52.         var c = (int)(uint.MaxValue * r.NextDouble());
  53.         var n = r.NextDouble() > 0.5;
  54.         var s = (byte)(29 * r.NextDouble());
  55.         return new Decimal(a, b, c, n, s);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement