Need a faster copy of the array

After a little reading, I found that there are some differences in the ways of copying arrays in java. For my application, I have a recursive tree of nodes, each of which contains an array of 2d-board (8x8).

Using profiler testing, it is best to use the java.util.Arrays.copyOf (array) method, which uses the built-in System.arraycopy system.

Even so, I spend 80% of my time creating new arrays. If anyone has any ideas on how to speed this up, I would appreciate it. Perhaps moving to an array of 64 elements rather than 8x8 will be faster. I checked this soon.

+6
source share
5 answers

I recently conducted an investigation in this regard (see my answer to my own question in Is there a way to create a primitive array without initialization ? , it could be called "Why is Array.copyOf so slow" or "Why Java is so slow") and even sent RFE to Oracle. The basic idea is that Java spends too much time on useless array initialization. It is about how Arrays.copyOf could be faster.

+1
source

The fact that you spend 80% of your time copying arrays means one of two things:

  • copying arrays is too slow;
  • you do almost nothing next to copying arrays.

Your copying performance can be advanced; instead, consider the architecture of your application, try to reduce the amount of data copied.

+3
source

System.arraycopy () is the best if your code should be clear.

However, if performance becomes a real bottleneck, you can see:

+3
source

you need algorithmic improvement. (do you follow the minimal chess algorithm?)

Ability to simply copy the link to each 8x8 array and add the "shared" flag to each array. Then copy the array only if you really modify the array. Since you do not change all arrays, this will significantly reduce the number of copies.

Another option would be to find a more compact representation for your 8x8 array (e.g. a bit of magic).

What do array entries contain?

+1
source

Thanks for answers. An 8x8 array consists of 9 array distributions and 1 initialization. Using an array of size 64 rather than 8x8, the distribution is only done once, and now I can skip initialization.

However, I will consider other ways to improve speed ... because faster copies of arrays mean that I can make more nodes: D.

Thank you all

0
source

All Articles