如何改进C#中的嵌套循环?

时间:2014-10-29 07:27:47

标签: c# loops

我有一个函数来计算costMatrix,如下面的代码所示。

public double[,] makeCostMatrixClassic(List<PointD> firstSeq, List<PointD> secondSeq)
{
    double[,] costMatrix = new double[firstSeq.Count, secondSeq.Count];
    costMatrix[0, 0] = Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[0]), 3);
    //  For D(n,1)
    for (int i = 0; i < firstSeq.Count; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            costMatrix[i, 0] += Math.Round(this.getEuclideanDistance(firstSeq[j], secondSeq[0]), 3);
        }
    }
    //  For D(1,m)
    for (int i = 0; i < secondSeq.Count; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            costMatrix[0, i] += Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[j]), 3);
        }
    }
    //  For D(n,m)
    for (int i = 1; i < firstSeq.Count; i++)
    {
        for (int j = 1; j < secondSeq.Count; j++)
        {
            double min = this.findMin(costMatrix[i - 1, j - 1], costMatrix[i - 1, j], costMatrix[i, j - 1]);
            costMatrix[i, j] = min + Math.Round(this.getEuclideanDistance(firstSeq[i], secondSeq[j]), 3);
        }
    }
    return costMatrix;
}

对于第3个循环(n,m),如果每个序列的“计数”为100万个点,我怎么能改善它的性能。

0 个答案:

没有答案