gpt4 book ai didi

.NET矩形数组: how to access in a loop?

转载 作者:行者123 更新时间:2023-12-02 15:09:40 26 4
gpt4 key购买 nike

基本上你有两种方法可以做到这一点:

for (int x = 0; x < UPPER_X; x++)
for (int y = 0; y < UPPER_Y; y++)
{
arr1[x, y] = get_value();
arr2[y, x] = get_value();
}

唯一的区别是在内循环中更改哪个变量:第一个或第二个。我听说不同语言的结果有所不同。

.NET 的正确顺序是什么?

最佳答案

您应该对您的特定情况进行基准测试以确定。

您可能会认为矩形数组(即连续分配的内存)没有区别,但根据此 MSDN article有一个区别:

You can get even better results by converting a multidimensional array into a single-dimensional array. If you don't mind the syntax, this can be trivial; just use one index as an offset. For example, the following declares a single-dimensional array to be used as a two-dimensional array:

double[] myArray = new double[ROW_DIM * COLUMN_DIM];

For indexing the elements of this array, use the following offset:

myArray[row * COLUMN_DIM + column];

This will undoubtedly be faster than an equivalent jagged or rectangular array.

关于.NET矩形数组: how to access in a loop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/405810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com