What is the fastest way to reorder an array

What is the fastest (real-time data processing application) for reordering (new indexes are always the same) array in JAVA:

for example: I have: double[] A = new double[] {1, 234, 12,99,0};

I need quickly: double[] B = new double[] {A[2], A[4], A[0],A[1],A[3]};

But maybe this is the most effective way to do it anyway?

Thank you very much for your feedback.

+1
source share
1 answer

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.

+1
source

Source: https://habr.com/ru/post/1235082/


All Articles