Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Last active December 20, 2015 05:49
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/6081593 to your computer and use it in GitHub Desktop.
Save Fhernd/6081593 to your computer and use it in GitHub Desktop.
Prueba del rendimiento de tipos de punto flotante y float en C#.
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