在c#中,多维数组double[,]和数组的数组double[][]有什么区别?
如果有区别,每一种的最佳用途是什么?
在c#中,多维数组double[,]和数组的数组double[][]有什么区别?
如果有区别,每一种的最佳用途是什么?
当前回答
我正在解析ildasm生成的.il文件,以构建用于进行转换的程序集、类、方法和存储过程的数据库。我遇到了下面的问题,打断了我的解析。
.method private hidebysig instance uint32[0...,0...]
GenerateWorkingKey(uint8[] key,
bool forEncryption) cil managed
Serge Lidin于2006年出版的《Expert . net 2.0 IL汇编器》一书,第8章,原始类型和签名,149-150页解释了这一点。
<type>[]被称为<type>的Vector,
<type>[<bounds> [<bounds>**]]被称为<type>的数组
**表示可重复,[]表示可选。
示例:Let <type> = int32。
1) int32[……]是一个具有未定义的下界和大小的二维数组
2) int32[2…5]是一个下界为2,大小为4的一维数组。
3) int32[0, 0…]是一个下界为0且大小未定义的二维数组。
Tom
其他回答
这可能在上面的回答中提到过,但没有明确地提到:对于锯齿数组,您可以使用array[row]引用整行数据,但这对于多维数组是不允许的。
我想从未来开始,我应该在这里补充一些。net 5的性能结果,因为从现在开始,它将成为每个人都使用的平台。
这些测试与约翰·雷德格伦(2009年)使用的测试相同。
我的结果。净5.0.1):
Debug:
(Jagged)
5.616 4.719 4.778 5.524 4.559 4.508 5.913 6.107 5.839 5.270
(Multi)
6.336 7.477 6.124 5.817 6.516 7.098 5.272 6.091 25.034 6.023
(Single)
4.688 3.494 4.425 6.176 4.472 4.347 4.976 4.754 3.591 4.403
Release(code optimizations on):
(Jagged)
2.614 2.108 3.541 3.065 2.172 2.936 1.681 1.724 2.622 1.708
(Multi)
3.371 4.690 4.502 4.153 3.651 3.637 3.580 3.854 3.841 3.802
(Single)
1.934 2.102 2.246 2.061 1.941 1.900 2.172 2.103 1.911 1.911
运行在一个6核3.7GHz AMD Ryzen 1600机器上。
看起来性能比率仍然大致相同。我想说,除非你真的很难优化,否则就使用多维数组,因为语法更容易使用。
前言:本评论旨在解决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.
除了其他答案之外,请注意,多维数组被分配为堆上的一个大块对象。这有一些含义:
Some multidimensional arrays will get allocated on the Large Object Heap (LOH) where their equivalent jagged array counterparts would otherwise not have. The GC will need to find a single contiguous free block of memory to allocate a multidimensional array, whereas a jagged array might be able to fill in gaps caused by heap fragmentation... this isn't usually an issue in .NET because of compaction, but the LOH doesn't get compacted by default (you have to ask for it, and you have to ask every time you want it). You'll want to look into <gcAllowVeryLargeObjects> for multidimensional arrays way before the issue will ever come up if you only ever use jagged arrays.
简单地说,多维数组类似于DBMS中的表。 Array of Array(锯齿状数组)允许每个元素保存另一个相同类型的可变长度数组。
因此,如果您确定数据结构看起来像一个表(固定的行/列),您可以使用多维数组。锯齿数组是固定的元素&每个元素可以容纳一个可变长度的数组
例如Psuedocode:
int[,] data = new int[2,2];
data[0,0] = 1;
data[0,1] = 2;
data[1,0] = 3;
data[1,1] = 4;
我们可以把上面的表格看成一个2x2的表格:
1 | 2 3 | 4
int[][] jagged = new int[3][];
jagged[0] = new int[4] { 1, 2, 3, 4 };
jagged[1] = new int[2] { 11, 12 };
jagged[2] = new int[3] { 21, 22, 23 };
可以把上面的代码看成每一行都有可变的列数:
1 | 2 | 3 | 4 11 | 12 21 | | 22, 23