[英]Multi-dimensional array vs. One-dimensional
這基本上是對這個問題的重述: Java:多維數組與一維數,但對於C#。
我有一定數量的元素可以作為網格存儲。 我應該使用數組[x * y]還是數組[x] [y]?
編輯:哦,所以有一維數組[x * y],多維數組[x,y]和鋸齒狀數組[x] [y],我可能想要鋸齒狀?
我對不合理的大型陣列進行了測試,並驚訝地看到Jagged數組([y] [x])似乎比單個維數陣列更快,手動乘法[y * ySize + x]。 並且多維數組[,]比較慢但不是那么多。
當然你必須測試你的特定陣列,但看起來差異並不大,所以你應該只使用適合你所做的最好的方法。
0.280 (100.0% | 0.0%) 'Jagged array 5,059x5,059 - 25,593,481'
| 0.006 (2.1% | 2.1%) 'Allocate'
| 0.274 (97.9% | 97.9%) 'Access'
0.336 (100.0% | 0.0%) 'TwoDim array 5,059x5,059 - 25,593,481'
| 0.000 (0.0% | 0.0%) 'Allocate'
| 0.336 (99.9% | 99.9%) 'Access'
0.286 (100.0% | 0.0%) 'SingleDim array 5,059x5,059 - 25,593,481'
| 0.000 (0.1% | 0.1%) 'Allocate'
| 0.286 (99.9% | 99.9%) 'Access'
0.552 (100.0% | 0.0%) 'Jagged array 7,155x7,155 - 51,194,025'
| 0.009 (1.6% | 1.6%) 'Allocate'
| 0.543 (98.4% | 98.4%) 'Access'
0.676 (100.0% | 0.0%) 'TwoDim array 7,155x7,155 - 51,194,025'
| 0.000 (0.0% | 0.0%) 'Allocate'
| 0.676 (100.0% | 100.0%) 'Access'
0.571 (100.0% | 0.0%) 'SingleDim array 7,155x7,155 - 51,194,025'
| 0.000 (0.1% | 0.1%) 'Allocate'
| 0.571 (99.9% | 99.9%) 'Access'
for (int i = 6400000; i < 100000000; i *= 2)
{
int size = (int)Math.Sqrt(i);
int totalSize = size * size;
GC.Collect();
ProfileTimer.Push(string.Format("Jagged array {0:N0}x{0:N0} - {1:N0}", size, totalSize));
ProfileTimer.Push("Allocate");
double[][] Jagged = new double[size][];
for (int x = 0; x < size; x++)
{
Jagged[x] = new double[size];
}
ProfileTimer.PopPush("Allocate", "Access");
double total = 0;
for (int trials = 0; trials < 10; trials++)
{
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
total += Jagged[y][x];
}
}
}
ProfileTimer.Pop("Access");
ProfileTimer.Pop("Jagged array");
GC.Collect();
ProfileTimer.Push(string.Format("TwoDim array {0:N0}x{0:N0} - {1:N0}", size, totalSize));
ProfileTimer.Push("Allocate");
double[,] TwoDim = new double[size,size];
ProfileTimer.PopPush("Allocate", "Access");
total = 0;
for (int trials = 0; trials < 10; trials++)
{
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
total += TwoDim[y, x];
}
}
}
ProfileTimer.Pop("Access");
ProfileTimer.Pop("TwoDim array");
GC.Collect();
ProfileTimer.Push(string.Format("SingleDim array {0:N0}x{0:N0} - {1:N0}", size, totalSize));
ProfileTimer.Push("Allocate");
double[] Single = new double[size * size];
ProfileTimer.PopPush("Allocate", "Access");
total = 0;
for (int trials = 0; trials < 10; trials++)
{
for (int y = 0; y < size; y++)
{
int yOffset = y * size;
for (int x = 0; x < size; x++)
{
total += Single[yOffset + x];
}
}
}
ProfileTimer.Pop("Access");
ProfileTimer.Pop("SingleDim array");
}
C#在使用鋸齒狀數組 ( array[][]
)方面有許多優點。 它們實際上通常會勝過多維數組。
話雖這么說,我個人會使用多維或鋸齒狀數組而不是單維數組,因為這會更緊密地匹配問題空間。 使用一維數組會增加實現的復雜性,但實際上並沒有帶來實際好處,特別是與2D數組相比,在內部時,它仍然只是一塊內存。
array[x,y]
優點:
- 運行時將為您執行更多檢查 。 將檢查每個索引訪問是否在允許的范圍內。 使用另一種方法,您可以像a[y*numOfColumns + x]
一樣輕松地執行smth,其中x可以超過“列數”,並且此代碼將提取一些錯誤的值而不會拋出異常。
- 更清晰的索引訪問 。 a[x,y]
比a[y*numOfColumns + x]
更清晰
array[x*y]
優點array[x*y]
:
- 更輕松地迭代整個陣列 。 您只需要一個循環而不是兩個循環。
贏家是......我更喜歡array[x,y]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.