Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created May 21, 2016 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/7cc07d0a995cf343e5664e102e7ad295 to your computer and use it in GitHub Desktop.
Save Fhernd/7cc07d0a995cf343e5664e102e7ad295 to your computer and use it in GitHub Desktop.
Prueba de rendimiento de la estructura TimeSpan.
using System;
using System.Diagnostics;
namespace Articulo.Cap06
{
public class RendimientoTimeSpan
{
public static void Main()
{
// Límite de pruebas:
const int LIM_PRUEBAS = 100000000;
// Creación e inicio de cronómetro:
Stopwatch cronometro1 = Stopwatch.StartNew();
// Creación de objetos TimeSpan a partir de TimeSpan.FromHours:
for (int numPrueba = 1; numPrueba <= LIM_PRUEBAS; ++numPrueba)
{
TimeSpan intervalo = TimeSpan.FromHours(1);
}
// Detiene el cronómetro:
cronometro1.Stop();
// Crea un nuevo contador:
Stopwatch cronometro2 = Stopwatch.StartNew();
// Creación de objetos con el constructor TimeSpan(Int32, Int32, Int32):
for(int numPrueba = 1; numPrueba <= LIM_PRUEBAS; ++numPrueba)
{
TimeSpan intervalo = new TimeSpan(1, 0, 0);
}
// Detiene el cronómetro:
cronometro2.Stop();
// Un nuevo cronómetro para la instanciación con caché:
Stopwatch cronometro3 = Stopwatch.StartNew();
// Creación de caché para objetos TimeSpan:
TimeSpan cache = new TimeSpan(1, 0, 0);
// Creación de objetos TimeSpan a partir de un caché:
for (int numPrueba = 1; numPrueba <= LIM_PRUEBAS; ++numPrueba)
{
TimeSpan intervalo = cache;
}
// Detiene el último cronómetro:
cronometro3.Stop();
// Visualización de resultados:
Console.WriteLine ("\nTimeSpan.FromHours(1): {0,8}ms", cronometro1.ElapsedMilliseconds);
Console.WriteLine ("new TimeSpan(1, 0, 0): {0,8}ms", cronometro2.ElapsedMilliseconds);
Console.WriteLine ("Caché: {0,24}ms\n", cronometro3.ElapsedMilliseconds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment