Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need of a faster array copy

After doing a bit of reading, I discovered that there are some differences in the ways you can copy arrays in java. For my application I have a recursive tree of nodes, each containing a 2d board array (8x8).

Through profiler testing, the best thing I could come up with is the java.util.Arrays.copyOf(array) method, which uses the native System.arraycopy.

Even with this, I am spending 80% of my time creating new arrays. If anyone has any ideas on how to speed this up I'd appreciate it. Perhaps going to a 64 item array rather than an 8x8 would be faster. I'll test this shortly.

like image 487
Chris Avatar asked Dec 16 '12 14:12

Chris


People also ask

What is array copy?

Copy(Array, Array, Int32) Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.

Which is used to copy an array?

Using clone() method. Using arraycopy() method. Using copyOf() method of Arrays class.

What does Arraycopy do in Java?

arraycopy. Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest .

Is Arraycopy fast?

arraycopy is a native call (that apparently uses memcpy). Even with the JNI overhead, System. arraycopy runs faster than Arrays. fill().


1 Answers

The fact that you are spending 80% time copying arrays means one of two things:

  1. array copying is too slow;
  2. you are barely doing anything else beside copying arrays.

Your copying performance may already be cutting-edge; consider the architecture of your application instead, try reducing the amount of copied data.

like image 197
Marko Topolnik Avatar answered Oct 13 '22 04:10

Marko Topolnik