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);
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uRGlhZ25vc3RpY3M7CgpuYW1lc3BhY2UgQXJ0aWN1bG8uQ2FwMDYKewogICAgcHVibGljIGNsYXNzIFJlbmRpbWllbnRvVGltZVNwYW4KICAgIHsKICAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCiAgICAgICAgewogICAgICAgICAgICAvLyBMw61taXRlIGRlIHBydWViYXM6CiAgICAgICAgICAgIGNvbnN0IGludCBMSU1fUFJVRUJBUyA9IDEwMDAwMDAwMDsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIENyZWFjacOzbiBlIGluaWNpbyBkZSBjcm9uw7NtZXRybzoKICAgICAgICAgICAgU3RvcHdhdGNoIGNyb25vbWV0cm8xID0gU3RvcHdhdGNoLlN0YXJ0TmV3KCk7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBDcmVhY2nDs24gZGUgb2JqZXRvcyBUaW1lU3BhbiBhIHBhcnRpciBkZSBUaW1lU3Bhbi5Gcm9tSG91cnM6CiAgICAgICAgICAgIGZvciAoaW50IG51bVBydWViYSA9IDE7IG51bVBydWViYSA8PSBMSU1fUFJVRUJBUzsgKytudW1QcnVlYmEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIFRpbWVTcGFuIGludGVydmFsbyA9IFRpbWVTcGFuLkZyb21Ib3VycygxKTsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgLy8gRGV0aWVuZSBlbCBjcm9uw7NtZXRybzoKICAgICAgICAgICAgY3Jvbm9tZXRybzEuU3RvcCgpOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gQ3JlYSB1biBudWV2byBjb250YWRvcjoKICAgICAgICAgICAgU3RvcHdhdGNoIGNyb25vbWV0cm8yID0gU3RvcHdhdGNoLlN0YXJ0TmV3KCk7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBDcmVhY2nDs24gZGUgb2JqZXRvcyBjb24gZWwgY29uc3RydWN0b3IgVGltZVNwYW4oSW50MzIsIEludDMyLCBJbnQzMik6CiAgICAgICAgICAgIGZvcihpbnQgbnVtUHJ1ZWJhID0gMTsgbnVtUHJ1ZWJhIDw9IExJTV9QUlVFQkFTOyArK251bVBydWViYSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgVGltZVNwYW4gaW50ZXJ2YWxvID0gbmV3IFRpbWVTcGFuKDEsIDAsIDApOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBEZXRpZW5lIGVsIGNyb27Ds21ldHJvOgogICAgICAgICAgICBjcm9ub21ldHJvMi5TdG9wKCk7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBVbiBudWV2byBjcm9uw7NtZXRybyBwYXJhIGxhIGluc3RhbmNpYWNpw7NuIGNvbiBjYWNow6k6CiAgICAgICAgICAgIFN0b3B3YXRjaCBjcm9ub21ldHJvMyA9IFN0b3B3YXRjaC5TdGFydE5ldygpOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gQ3JlYWNpw7NuIGRlIGNhY2jDqSBwYXJhIG9iamV0b3MgVGltZVNwYW46CiAgICAgICAgICAgIFRpbWVTcGFuIGNhY2hlID0gbmV3IFRpbWVTcGFuKDEsIDAsIDApOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gQ3JlYWNpw7NuIGRlIG9iamV0b3MgVGltZVNwYW4gYSBwYXJ0aXIgZGUgdW4gY2FjaMOpOgogICAgICAgICAgICBmb3IgKGludCBudW1QcnVlYmEgPSAxOyBudW1QcnVlYmEgPD0gTElNX1BSVUVCQVM7ICsrbnVtUHJ1ZWJhKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBUaW1lU3BhbiBpbnRlcnZhbG8gPSBjYWNoZTsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgLy8gRGV0aWVuZSBlbCDDumx0aW1vIGNyb27Ds21ldHJvOgogICAgICAgICAgICBjcm9ub21ldHJvMy5TdG9wKCk7CiAgICAgICAgICAgIAogICAgICAgICAgICAKICAgICAgICAgICAgLy8gVmlzdWFsaXphY2nDs24gZGUgcmVzdWx0YWRvczoKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJcblRpbWVTcGFuLkZyb21Ib3VycygxKTogezAsOH1tcyIsIGNyb25vbWV0cm8xLkVsYXBzZWRNaWxsaXNlY29uZHMpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoIm5ldyBUaW1lU3BhbigxLCAwLCAwKTogezAsOH1tcyIsIGNyb25vbWV0cm8yLkVsYXBzZWRNaWxsaXNlY29uZHMpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoIkNhY2jDqTogezAsMjR9bXNcbiIsIGNyb25vbWV0cm8zLkVsYXBzZWRNaWxsaXNlY29uZHMpOwogICAgICAgIH0KICAgIH0KfQ==