需要帮助改善循环性能

时间:2016-10-19 05:42:30

标签: c#

我正在寻找降低此循环成本的方法。将新项添加到阵列时会出现瓶颈。我跑得很近。千万次迭代,所以任何性能的提升,无论多小,都会有很长的路要走。

int[][] coordinates;

public void RefactorCoordinates()
{
    try
    {
        coordinates = new int[10000000][];
        int nextIndex = 0;
        double width = OffsetWidth;
        double height = OffsetHeight;
        double depth = OffsetDepth;
        for (int z = 0; z < width; ++z)
        {
            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < depth; ++x)
                {
                    coordinates[nextIndex] = new int[] { z, y, x };
                    nextIndex++;
                }
            }
        }
        Array.Resize(ref coordinates, nextIndex);
    }
    catch(Exception ex)
    {
        ex.ToString();
    }
}

1 个答案:

答案 0 :(得分:0)

事实证明,通过结构而不是数组表示数据可以缓解大部分性能问题。我不确定为什么这会更快,但无论如何都有效。

相关问题