我目前正在开发一个应用程序,负责计算锯齿状阵列的随机排列.
目前,应用程序中的大部分时间都花费在每次迭代中复制数组(总共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;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法,安全或不安全,以达到与上述相同的效果,更快?