Advertisement
Fhernd

RendimientoTimeSpan.cs

May 20th, 2016
6,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace Articulo.Cap06
  5. {
  6.     public class RendimientoTimeSpan
  7.     {
  8.         public static void Main()
  9.         {
  10.             // Límite de pruebas:
  11.             const int LIM_PRUEBAS = 100000000;
  12.            
  13.             // Creación e inicio de cronómetro:
  14.             Stopwatch cronometro1 = Stopwatch.StartNew();
  15.            
  16.             // Creación de objetos TimeSpan a partir de TimeSpan.FromHours:
  17.             for (int numPrueba = 1; numPrueba <= LIM_PRUEBAS; ++numPrueba)
  18.             {
  19.                 TimeSpan intervalo = TimeSpan.FromHours(1);
  20.             }
  21.            
  22.             // Detiene el cronómetro:
  23.             cronometro1.Stop();
  24.            
  25.             // Crea un nuevo contador:
  26.             Stopwatch cronometro2 = Stopwatch.StartNew();
  27.            
  28.             // Creación de objetos con el constructor TimeSpan(Int32, Int32, Int32):
  29.             for(int numPrueba = 1; numPrueba <= LIM_PRUEBAS; ++numPrueba)
  30.             {
  31.                 TimeSpan intervalo = new TimeSpan(1, 0, 0);
  32.             }
  33.            
  34.             // Detiene el cronómetro:
  35.             cronometro2.Stop();
  36.            
  37.             // Un nuevo cronómetro para la instanciación con caché:
  38.             Stopwatch cronometro3 = Stopwatch.StartNew();
  39.            
  40.             // Creación de caché para objetos TimeSpan:
  41.             TimeSpan cache = new TimeSpan(1, 0, 0);
  42.            
  43.             // Creación de objetos TimeSpan a partir de un caché:
  44.             for (int numPrueba = 1; numPrueba <= LIM_PRUEBAS; ++numPrueba)
  45.             {
  46.                 TimeSpan intervalo = cache;
  47.             }
  48.            
  49.             // Detiene el último cronómetro:
  50.             cronometro3.Stop();
  51.            
  52.            
  53.             // Visualización de resultados:
  54.             Console.WriteLine ("\nTimeSpan.FromHours(1): {0,8}ms", cronometro1.ElapsedMilliseconds);
  55.             Console.WriteLine ("new TimeSpan(1, 0, 0): {0,8}ms", cronometro2.ElapsedMilliseconds);
  56.             Console.WriteLine ("Caché: {0,24}ms\n", cronometro3.ElapsedMilliseconds);
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement