在迭代大约3000个float
值并运行内循环的数据集时,此代码以秒为单位运行前10%,但需要20分钟才能完成最后90%。创建的new
个对象的数量已尽可能减少。
在外部循环中以固定间隔调用GC.Collect();
然后GC.WaitForPendingFinalizers();
导致没有变化。
如何在整个循环中保持快速性能?使用实际的类而不是Object。
公共方法:
public static float [] Compute
( ref float [] sourceData , ref Object [] referenceData,
ref float [] progress )
{
int count = 0;
float [] result = new float [sourceData.length];
float inverseLength = 1f / ((float) sourceData.length);
for ( int i = 0; i < sourceData.length; i++ )
{
progress [0] = ( (float) i ) * inverseLength ;
result[i] = computeDatum (ref sourceData [i], ref referenceData);
if (count++ == 100)
{
count = 0;
GC.Collect ( );
GC.WaitForPendingFinalizers ( );
}
}
return result;
}
私有方法中的内部循环:
private static float computeDatum
( ref float sourceDatum , ref Object [] referenceData)
{
Object workingObject = new Object();
float result = 0f;
foreach ( Object o in referenceData )
{
workingObject = new Object(o);
//...perform transformation operations on workingObject...
//...store results of computation with result...
}
return result;
}