在c#中,多维数组double[,]和数组的数组double[][]有什么区别?
如果有区别,每一种的最佳用途是什么?
在c#中,多维数组double[,]和数组的数组double[][]有什么区别?
如果有区别,每一种的最佳用途是什么?
当前回答
这可能在上面的回答中提到过,但没有明确地提到:对于锯齿数组,您可以使用array[row]引用整行数据,但这对于多维数组是不允许的。
其他回答
前言:本评论旨在解决okutane提供的关于锯齿数组和多维数组之间性能差异的答案。
一种类型因为方法调用而比另一种类型慢的断言是不正确的。其中一种比另一种慢,因为它的边界检查算法更复杂。您可以通过查看编译后的程序集而不是IL轻松验证这一点。例如,在我的4.5安装中,访问存储在ecx指向的二维数组中的元素(通过edx中的指针),索引存储在eax和edx中,如下所示:
sub eax,[ecx+10]
cmp eax,[ecx+08]
jae oops //jump to throw out of bounds exception
sub edx,[ecx+14]
cmp edx,[ecx+0C]
jae oops //jump to throw out of bounds exception
imul eax,[ecx+0C]
add eax,edx
lea edx,[ecx+eax*4+18]
Here, you can see that there's no overhead from method calls. The bounds checking is just very convoluted thanks to the possibility of non-zero indexes, which is a functionality not on offer with jagged arrays. If we remove the sub, cmp, and jmps for the non-zero cases, the code pretty much resolves to (x*y_max+y)*sizeof(ptr)+sizeof(array_header). This calculation is about as fast (one multiply could be replaced by a shift, since that's the whole reason we choose bytes to be sized as powers of two bits) as anything else for random access to an element.
Another complication is that there are plenty of cases where a modern compiler will optimize away the nested bounds-checking for element access while iterating over a single-dimension array. The result is code that basically just advances an index pointer over the contiguous memory of the array. Naive iteration over multi-dimensional arrays generally involves an extra layer of nested logic, so a compiler is less likely to optimize the operation. So, even though the bounds-checking overhead of accessing a single element amortizes out to constant runtime with respect to array dimensions and sizes, a simple test-case to measure the difference may take many times longer to execute.
多维数组是(n-1)维矩阵。
所以int[,] square = new int[2,2]是一个方阵2x2, int[,] cube = new int[3,3,3]是一个立方-方阵3x3。比例不是必需的。
交错数组只是数组的数组——每个单元格包含一个数组的数组。
所以MDA是成比例的,JD可能不是!每个单元格可以包含任意长度的数组!
交错数组是数组的数组,或者每一行包含一个自己的数组的数组。
这些数组的长度可以不同于其他行的长度。
声明和分配数组的数组
与常规多维数组相比,锯齿数组声明的唯一不同之处在于,我们不只有一对括号。对于锯齿状数组,每个维度都有一对括号。我们这样分配它们:
int [] [] exampleJaggedArray; jaggedArray = new int[2][]; jaggedArray[0] = new int[5]; jaggedArray[1] = new int[3];
初始化数组的数组
int[][] exampleJaggedArray = { New int[] {5,7,2}, New int[] {10,20,40}, 新的int[] {3,25} };
内存分配
锯齿数组是引用的聚合。锯齿状数组不直接包含任何数组,而是有指向它们的元素。大小是未知的,这就是为什么CLR只保留对内部数组的引用。在我们为锯齿状数组的一个数组元素分配内存之后,引用开始指向动态内存中新创建的块。
变量exampleJaggedArray存储在程序的执行堆栈中,并指向动态内存中的一个块,该块包含对内存中其他三个块的三个引用序列;它们每个都包含一个整数数组——锯齿数组的元素:
这可能在上面的回答中提到过,但没有明确地提到:对于锯齿数组,您可以使用array[row]引用整行数据,但这对于多维数组是不允许的。
使用基于John Leidegren的测试,我使用。net 4.7.2对结果进行了基准测试,这是与我的目的相关的版本,我认为我可以分享。我最初是从dotnet核心GitHub存储库中的这条注释开始的。
随着数组大小的变化,性能似乎有很大变化,至少在我的设置中是这样,1个处理器xeon, 4physical 8logical。
W =初始化一个数组,并将int I * j放入其中。 Wr = do w,然后在另一个循环中设置int x为[i,j]
随着数组大小的增长,多维似乎表现得更好。
Size | rw | Method | Mean | Error | StdDev | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op |
---|---|---|---|---|---|---|---|---|---|
1800*500 | w | Jagged | 2.445 ms | 0.0959 ms | 0.1405 ms | 578.1250 | 281.2500 | 85.9375 | 3.46 MB |
1800*500 | w | Multi | 3.079 ms | 0.2419 ms | 0.3621 ms | 269.5313 | 269.5313 | 269.5313 | 3.43 MB |
2000*4000 | w | Jagged | 50.29 ms | 3.262 ms | 4.882 ms | 5937.5000 | 3375.0000 | 937.5000 | 30.62 MB |
2000*4000 | w | Multi | 26.34 ms | 1.797 ms | 2.690 ms | 218.7500 | 218.7500 | 218.7500 | 30.52 MB |
2000*4000 | wr | Jagged | 55.30 ms | 3.066 ms | 4.589 ms | 5937.5000 | 3375.0000 | 937.5000 | 30.62 MB |
2000*4000 | wr | Multi | 32.23 ms | 2.798 ms | 4.187 ms | 285.7143 | 285.7143 | 285.7143 | 30.52 MB |
1000*2000 | wr | Jagged | 11.18 ms | 0.5397 ms | 0.8078 ms | 1437.5000 | 578.1250 | 234.3750 | 7.69 MB |
1000*2000 | wr | Multi | 6.622 ms | 0.3238 ms | 0.4847 ms | 210.9375 | 210.9375 | 210.9375 | 7.63 MB |
更新:最后两个测试用双[,]代替int[,]。考虑到误差,这种差异显得很显著。对于int,锯齿与md的平均比率在1.53x和1.86x之间,对于双精度,它是1.88x和2.42x。
Size | rw | Method | Mean | Error | StdDev | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op |
---|---|---|---|---|---|---|---|---|---|
1000*2000 | wr | Jagged | 26.83 ms | 1.221 ms | 1.790 ms | 3062.5000 | 1531.2500 | 531.2500 | 15.31 MB |
1000*2000 | wr | Multi | 12.61 ms | 1.018 ms | 1.524 ms | 156.2500 | 156.2500 | 156.2500 | 15.26 MB |