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

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

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

我的数组克隆例程如下:

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

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

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

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

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

您可以将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获取要为count参数复制的字节数,而不是数组元素的数量。

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