将锯齿状数组的值克隆到第二个数组的速度非常快?

时间:2011-01-12 15:44:29

标签: c# arrays performance

我目前正在开发一个负责计算锯齿状阵列随机排列的应用程序。

目前,应用程序中的大部分时间都花费在每次迭代中复制数组(总共100万次迭代)。在我当前的系统上,整个过程需要50秒才能完成,其中39秒用于克隆阵列。

我的数组克隆例程如下:

    public static int[][] CopyArray(this int[][] source)
    {
        int[][] destination = new int[source.Length][];
        // For each Row
        for (int y = 0; y < source.Length; y++)
        {
            // Initialize Array
            destination[y] = new int[source[y].Length];
            // For each Column
            for (int x = 0; x < destination[y].Length; x++)
            {
                destination[y][x] = source[y][x];
            }
        }
        return destination;
    }

有没有办法,安全或不安全,以达到与上述相同的效果,更快?

4 个答案:

答案 0 :(得分:21)

这些中的任何一个都适合你。它们都在大约相同的时间内运行,并且都比你的方法快得多。

// 100 passes on a int[1000][1000] set size

// 701% faster than original (14.26%)
static int[][] CopyArrayLinq(int[][] source)
{
    return source.Select(s => s.ToArray()).ToArray();
}

// 752% faster than original (13.38%)
static int[][] CopyArrayBuiltIn(int[][] source)
{
    var len = source.Length;
    var dest = new int[len][];

    for (var x = 0; x < len; x++)
    {
        var inner = source[x];
        var ilen = inner.Length;
        var newer = new int[ilen];
        Array.Copy(inner, newer, ilen);
        dest[x] = newer;
    }

    return dest;
}

答案 1 :(得分:0)

如何对数组进行序列化/反序列化,如果使用内存流和二进制序列化,我认为它应该非常快。

答案 2 :(得分:0)

您可以将Array.Clone用于内循环:

public static int[][] CopyArray(this int[][] source)
{
    int[][] destination = new int[source.Length][];
    // For each Row
    for(int y = 0;y < source.Length;y++)
    {
        destination[y] = (int[])source[y].Clone();
    }
    return destination;
}

内循环的另一个替代方案是Buffer.BlockCopy,但我没有测量它对Array.Clone的性能 - 也许它更快:

destination[y] = new int[source[y].Length];
Buffer.BlockCopy(source[y], 0, destination[y], 0, source[y].Length * 4);

编辑:Buffer.BlockCopy bytes 的数字复制为count参数,而不是数组元素的数量。

答案 3 :(得分:0)

复制对象的更快方法是不要复制它们 - 你考虑过这个选项吗?如果您只需要生成排列,则无需在每个排列上复制数据 - 只需更改数组即可。如果您需要继续以前的呼叫结果,这种方法将无法正常工作。在任何情况下,请检查您的代码,看看您是否没有多次复制数据。