Last active
December 20, 2015 05:49
-
-
Save Fhernd/6081593 to your computer and use it in GitHub Desktop.
Prueba del rendimiento de tipos de punto flotante y float en C#.
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
class doubleVSdecimal | |
{ | |
static void Main() | |
{ | |
Random aleatorio = new Random(); | |
decimal[] decimales = new decimal[100]; | |
double[] doubles = new double[100]; | |
for (int i = 0; i < 100; i++) | |
{ | |
decimales[i] = GenerarDecimal(aleatorio); | |
doubles[i] = aleatorio.NextDouble(); | |
} | |
System.Diagnostics.Stopwatch cronometro = new System.Diagnostics.Stopwatch(); | |
decimal acumuladorSumaDecimales = 0; | |
cronometro.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
acumuladorSumaDecimales += decimales[i]; | |
} | |
cronometro.Stop(); | |
Console.WriteLine("Tiempo transcurrido: {0}", cronometro.Elapsed.TotalMilliseconds); // 0.0005 (puede variar) | |
double acumuladorSumaDoubles = 0; | |
cronometro.Reset(); | |
cronometro.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
acumuladorSumaDoubles += doubles[i]; | |
} | |
cronometro.Stop(); | |
Console.WriteLine("Tiempo transcurrido: {0}", cronometro.Elapsed.TotalMilliseconds); // 0.0005 (puede variar) | |
Console.ReadKey(); | |
} | |
// Este método fue tomado desde [4] | |
public static decimal GenerarDecimal(Random r) | |
{ | |
var a = (int)(uint.MaxValue * r.NextDouble()); | |
var b = (int)(uint.MaxValue * r.NextDouble()); | |
var c = (int)(uint.MaxValue * r.NextDouble()); | |
var n = r.NextDouble() > 0.5; | |
var s = (byte)(29 * r.NextDouble()); | |
return new Decimal(a, b, c, n, s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment