I have a method in Java that I'm using to partition byte[] arrays into a number of fixed-size sub-arrays. It's not necessary for the last array to be that size, but all preceding ones must be, and the maximum size of a sub array is the partition size.
Here's the code I've been using:
private static byte[][] partition(byte[] in, int partitionSize) {
int partitionCount = (int)Math.ceil((double)in.length / (double) partitionSize);
byte[][] temp = new byte[partitionCount][partitionSize];
for (int i = 0; i < partitionCount; i++) {
if (in.length < (partitionSize * (i + 1))) {
temp[i] = new byte[(in.length - (partitionSize * i))];
}
for(int j = 0; (j < partitionSize && (partitionSize * i + j) < in.length); j++) {
temp[i][j] = in[(partitionSize * i + j)];
}
}
return temp;
}
Are there any obvious ways I could optimize this for speed? The partitionSize I've been using so far is 971, and even though I was expecting it to take a while for large byte arrays (around a gigabyte in size) I'm hoping to be able to squeeze out a bit more performance.
Array.forEach
? \$\endgroup\$row * row_length + column
\$\endgroup\$