I'm trying to build an extension method that will allow me to subdivide a huge array into much smaller ones. My current code is the following but it still takes about 2s to generate with an array of size 512x512x512 and the size of the sub cubes set to 32x32x32
public static T[][][][] Divide<T>(this T[][][] array, int xSize, int ySize, int zSize)
{
int numberOfCubesInAxisX = array.Length / xSize;
int numberOfCubesInAxisY = array[0].Length / ySize;
int numberOfCubesInAxisZ = array[0][0].Length / zSize;
T[][][][] arrayDivided = new T[numberOfCubesInAxisX * numberOfCubesInAxisY * numberOfCubesInAxisZ][][][];
int index = 0;
while (index < arrayDivided.Length)
{
int xIndex = index / numberOfCubesInAxisZ / numberOfCubesInAxisX;
int yIndex = index / numberOfCubesInAxisZ % numberOfCubesInAxisY;
int zIndex = index % numberOfCubesInAxisZ;
arrayDivided[index] = new T[xSize][][];
for (int x = 0; x < xSize; x++)
{
arrayDivided[index][x] = new T[ySize][];
for (int y = 0; y < ySize; y++)
{
arrayDivided[index][x][y] = new T[zSize];
for (int z = 0; z < zSize; z++)
{
arrayDivided[index][x][y][z] = array[x + (xIndex * xSize)][y + (yIndex * ySize)][z + (zIndex * zSize)];
}
}
}
index++;
}
return arrayDivided;
}
If anyone have any clue on how to optimize it, i'll be glad to hear it! Thanks!
[,,,]instead of[][][]for each cube. Additionally a pair of some math andArray.Copycalls will do the magic. AlsoBuffer.BlockCopyis the fastest for non-generic primitive-typed solution. Potentially you can speed up this by ~100 times. \$\endgroup\$Tyou can't useBuffer.BlockCopybut canArray.Copy. First copies array segment only as bytes thus you must know the exact item size in bytes.Array.Copywill do it forTfluently, a bit slower but a payment for Generic method nature. \$\endgroup\$Buffer.BlockCopyoperation. For large data set single-dimentional array is best choice. Because calculating item address likex * ySize * zSize + y * zSize + zis always faster than go through 3 index-bound-checks. \$\endgroup\$