Der überraschende Ausgang des Code unten zeigt Arithmetik auf Doppel fast 100% schneller als auf lange:Warum C# -Arithmetik bei Doppel scheint schneller als Arithmetik bei langen?
Test_DivOperator Arithmetik gemessene Zeit Float: 15974,5024 ms.
Test_DivOperator Ganzzahl arithmetische gemessene Zeit: 28548.183 ms.
Die verwendeten Buildeinstellungen sind .Net4.5 C# 5.0 (Platform Ziel: x64)
Die verwendete Hardware ist Intel Core i5-2520M (Lauf Windows7 64Bit)
Hinweis: Der verwendete Operator (hier ist Division) wirkt sich auf die Ergebnisse aus, Division maximiert diese Beobachtung
const int numOfIterations = 1; //this value takes memory access out of the game
const int numOfRepetitions = 500000000; //CPU bound application
Random rand = new Random();
double[] Operand1 = new double[numOfIterations];
double[] Operand2 = new double[numOfIterations];
double[] Operand3 = new double[numOfIterations];
long[] Int64Operand1 = new long[numOfIterations];
long[] Int64Operand2 = new long[numOfIterations];
long[] Int64Operand3 = new long[numOfIterations];
for (int i = 0; i < numOfIterations; i++)
{
Operand1[i]=(rand.NextDouble() * 100);
Operand2[i]=(rand.NextDouble() * 80);
Operand3[i]=(rand.NextDouble() * 17);
Int64Operand1[i] = (long)Operand1[i];
Int64Operand2[i] = (long)Operand2[i]+1;
Int64Operand3[i] = (long)Operand3[i]+1;
}
double[] StdResult = new double[numOfIterations];
long[] NewResult = new long[numOfIterations];
TimeSpan begin = Process.GetCurrentProcess().TotalProcessorTime;
for (int j = 0; j < numOfRepetitions; j++)
{
for (int i = 0; i < numOfIterations; i++)
{
double result = Operand1[i]/Operand2[i];
result = result/Operand3[i];
StdResult[i]=(result);
}
}
TimeSpan end = Process.GetCurrentProcess().TotalProcessorTime;
Console.WriteLine("Test_DivOperator Float arithmetic measured time: " + (end - begin).TotalMilliseconds + " ms.");
begin = Process.GetCurrentProcess().TotalProcessorTime;
for (int j = 0; j < numOfRepetitions; j++)
{
for (int i = 0; i < numOfIterations; i++)
{
long result = Int64Operand1[i]/Int64Operand2[i];
result = result/Int64Operand3[i];
NewResult[i]=(result);
}
}
end = Process.GetCurrentProcess().TotalProcessorTime;
Console.WriteLine("Test_DivOperator Integer arithmetic measured time: " + (end - begin).TotalMilliseconds + " ms.");
Das Speichern von Elementen in einem Array mit nicht definierter Länge kann aufgrund der Neuzuweisung ebenfalls einen Overhead verursachen. Dies führt eine zusätzliche Variable in Ihrem Test ein. – GolezTrol
Weiterführendes Lesematerial: [Optimierung von Ganzzahl-Divisionen] (http://www.codeproject.com/Articles/17480/Optimizing-integer-divisions-with-Multiply-Shift-i). – GolezTrol
@GolezTrol Zuweisung erfolgt nicht innerhalb der gemessenen Zeit, alle Arrays sind von fester Größe .. auch die beiden Schleifen sind identisch. –