【发布时间】:2011-06-07 22:18:00
【问题描述】:
我目前正在开发一个负责计算锯齿状数组的随机排列的应用程序。
目前,应用程序中的大部分时间都用于在每次迭代中复制数组(总共 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;
}
有什么方法,安全或不安全,可以更快地达到与上述相同的效果吗?
【问题讨论】:
-
您是否尝试过使用多维数组而不是锯齿状数组,即
static int[,]而不是static int [][]?
标签: c# arrays performance