Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将锯齿状数组的值克隆到第二个数组的极快方法?_C#_Arrays_Performance - Fatal编程技术网

C# 将锯齿状数组的值克隆到第二个数组的极快方法?

C# 将锯齿状数组的值克隆到第二个数组的极快方法?,c#,arrays,performance,C#,Arrays,Performance,我目前正在开发一个应用程序,负责计算锯齿阵列的随机排列 目前,应用程序中的大部分时间用于在每次迭代中复制阵列(总共一百万次迭代)。在我当前的系统上,整个过程需要50秒才能完成,其中39秒用于克隆阵列 我的阵列克隆例程如下所示: public static int[][] CopyArray(this int[][] source) { int[][] destination = new int[source.Length][]; // For ea

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

目前,应用程序中的大部分时间用于在每次迭代中复制阵列(总共一百万次迭代)。在我当前的系统上,整个过程需要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;
    }
publicstaticint[]]CopyArray(此int[]]source)
{
int[]destination=newint[source.Length][];
//每行
for(int y=0;y

有没有安全或不安全的方法可以更快地达到上述效果?

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

可以使用
数组。克隆
进行内部循环:

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;
}

编辑:为count参数获取要复制的字节数,而不是数组元素数。

这两种方法都适用于您。它们运行的时间大致相同,并且都比您的方法快得多

// 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;
}
//在int[1000][1000]集大小上传递100次
//比原来的速度快701%(14.26%)
静态int[]copyraraylinq(int[]source)
{
返回source.Select(s=>s.ToArray()).ToArray();
}
//比原来的速度快752%(13.38%)
静态int[]copyraraybuiltin(int[]source)
{
var len=源长度;
var dest=新整数[len][];
对于(变量x=0;x
复制对象的更快方法是根本不复制它们-您考虑过这个选项吗?如果只需要生成排列,则不需要在每个排列上复制数据,只需更改数组即可。如果您需要保留以前调用的结果,这种方法将无法很好地工作。无论如何,请检查您的代码,看看您是否已经多次复制了数据。

我尝试了您的第一个解决方案。不幸的是,它只减少了整个运行时间的几秒钟。我来试试你的第二个解决方案。你的第二个选择与第一个相当相似。与我原来的方法相比,只减少了一秒或两秒的总时间。序列化可能比他当前的实现慢几个数量级。您是否尝试使用多维数组而不是锯齿数组,即
静态int[,]
而不是
静态int[][]
?我需要数组的单独副本,因为每个排列都需要有一个原始的工作集。我发现这一点很轻松:顺便说一句,它看起来很容易扩展到更多维度:返回source.Select(s=>s.Select(p=>p.ToArray()).ToArray()).ToArray();
// 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;
}