Created
May 21, 2016 02:40
-
-
Save Fhernd/7cc07d0a995cf343e5664e102e7ad295 to your computer and use it in GitHub Desktop.
Prueba de rendimiento de la estructura TimeSpan.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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