I doubt you can do better than your current approach
double[] B = new double[] {A[2], A[4], A[0], A[1], A[3]};
The likely candidates for other sequences might be the forms Arrays.copyOf or Arrays.copyOfRange , but the minimum amount of work you have to do here is to:
- create a new array
- random access to each element of the array
There is a small chance that you can improve a bit with very specific read and write orders (to take advantage of the cache lines), one hunch is that it reads completely in order and writes almost everything in ascending order:
double[] B = new double[A.length]; B[2] = A[0]; B[3] = A[1]; B[4] = A[3]; B[0] = A[2]; B[1] = A[4];
But I have no strong hopes that this is noticeably better. If you are at the point where you are trying to eliminate or optimize L1 / L2 cache images, it's time to start micro-benchmarking, and the real answer is you should experiment.
source share