0
我有一个简单的过程,循环一组点数据并做线性插值来'填写'数据,我想知道是否有人可以帮助给我的代码提供一些性能提示,并显示我可以优化的区域代码,谢谢!线性互补,可以优化运行速度吗?
这里代码:
static internal Point3D[] _data;
static internal Point3dTree _kd;
static internal int _interpolation_count = 0;
static internal int _iteration_count = 0;
static Dictionary<int, Point3D> Interpolated_Values = new Dictionary<int, Point3D>();
static internal int _threasindex;
static internal double[] _threasholds = new double[]
{
0.5,
1.0,
1.5,
20.5
};
static internal double Interpolate(double x, double x0, double x1, double y0, double y1)
{
if ((x1 - x0) == 0)
return (y0 + y1)/2;
return y0 + (x - x0) * (y1 - y0)/(x1 - x0);
}
static void Main(string[] args)
{
using (new ProceduralTimer("Loading data"))
_data = LasReader.GetData(@"C:\WindowsLP\SAMPLE_PROJECT\brisport2\area_cov.las");
using (new ProceduralTimer("Bulding Kd tree"))
_kd = new Point3dTree(_data, false);
List<Point3D> InterpolatedData = _data.ToList();
_data = null;
using (new ProceduralTimer("Processing"))
{
int i = 0;
var neighbours = new List<Point3D>();
for (; i < InterpolatedData.Count; i++)
{
@rescan:
neighbours = _kd.NearestNeighbours(new KdTreeNode<Point3D>(InterpolatedData[i]), _threasholds[_threasindex % _threasholds.Length]);
if (neighbours.Count < 4 && _threasindex < _threasholds.Length)
{
_threasindex++;
_iteration_count++;
goto rescan;
}
else
{
if (neighbours.Count >= 4)
{
double[] xvalues = neighbours.Select(_ => _.X).ToArray();
double[] yvalues = neighbours.Select(_ => _.Y).ToArray();
double[] zvalues = neighbours.Select(_ => _.Z).ToArray();
Point3D pt = new Point3D();
pt.X = Math.Round(Interpolate(InterpolatedData[i].X, xvalues[0], xvalues[1], xvalues[2], xvalues[3]), 2);
pt.Y = Math.Round(Interpolate(InterpolatedData[i].Y, yvalues[0], yvalues[1], yvalues[2], yvalues[3]), 2);
pt.Z = Math.Round(Interpolate(InterpolatedData[i].Z, zvalues[0], zvalues[1], zvalues[2], zvalues[3]), 2);
Interpolated_Values[i] = pt;
_interpolation_count++;
}
_threasindex = 0;
}
}
}
这个问题可能是更适合于[代码审查堆栈交易所(http://codereview.stackexchange.com/ )。 –
根据Don Knuth的说法,“为什么你需要优化代码?”,[过早优化是万恶之源](http://en.wikiquote.org/wiki/) Donald_Knuth)。如果你做**需要优化代码,你是否需要减少时间或空间的使用,并且对于其中一个,你准备增加多少以减少另一个? – Simon
您可以将for循环转换为Parallel.ForEach方法。此外,您可以用do while循环替换goto以提高可读性。 – user467384