What's better in regards to performance? type[,] or type[][]?

前端 未结 4 797
野的像风
野的像风 2021-01-18 23:27

Is it more performant to have a bidimensional array (type[,]) or an array of arrays (type[][]) in C#?

Particularly for initial allocation a

4条回答
  •  自闭症患者
    2021-01-19 00:24

    I believe that [,] can allocate one contiguous chunk of memory, while [][] is N+1 chunk allocations where N is the size of the first dimension. So I would guess that [,] is faster on initial allocation.

    Access is probably about the same, except that [][] would involve one extra dereference. Unless you're in an exceptionally tight loop it's probably a wash. Now, if you're doing something like image processing where you are referencing between rows rather than traversing row by row, locality of reference will play a big factor and [,] will probably edge out [][] depending on your cache size.

    As Marc Gravell mentioned, usage is key to evaluating the performance...

提交回复
热议问题