J'ai couru un test sur les tableaux déraisonnablement grandes et été surpris de voir que les tableaux crénelées ([y] [x]) semblent être plus rapide que la tableau à une dimension avec multiplication manuelle [y * ySize + x]. Et les tableaux multidimensionnels [,] sont plus lents mais pas tellement. Bien sûr, vous devrez tester sur vos tableaux en particulier, mais il semblerait que le différent n'est pas beaucoup, donc vous devriez simplement utiliser quelle approche correspond à ce que vous faites le mieux.
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");
}
Je marquer le tableau '' multidimensional' comme tableau [x, y] 'au lieu de' array [x] [y] ' – Snowbear