Cloning int [] - did anyone get a faster suggestion?

I am looking at a profile where one of the processor's hot spots is a function consisting in cloning the final static int []. You may ask: "Why?" callers use the results as starting points for the hash process.

In other words, the code should execute (logically):

  • create a new array
  • get hash seeds (as much as the size of the array)
  • put the values ​​in a new array calculated from hash seeds. This is an iterative algorithm, so it is beneficial that the seeds begin in the array - and therefore the idea is to start with a clone of the seed array.

Before I either give up or start using micro-lenses, I will post this question if anyone has any special knowledge about what is under the hood with clone () compared to Arrays.copyOf, and not just the new and arrasicopy.

+4
source share
4 answers

Arrays.copyOf uses System.arraycopy , but first adds some fixes

 public static int[] copyOf(int[] original, int newLength) { if (0 < = newLength) { return copyOfRange(original, 0, newLength); } throw new NegativeArraySizeException(); } public static int[] copyOfRange(int[] original, int start, int end) { if (start < = end) { if (original.length >= start && 0 < = start) { int length = end - start; int copyLength = Math.min(length, original.length - start); int[] copy = new int[length]; System.arraycopy(original, start, copy, 0, copyLength); return copy; } throw new ArrayIndexOutOfBoundsException(); } throw new IllegalArgumentException(); } 

I got this from http://www.docjar.com/docs/api/java/util/Arrays.html , so it would be a little better to use this than Arrays.copyOf

 int[] newone = new int[orig.length]; System.arraycopy(orig, 0, newone , 0, orig.length); 

although it would actually be better to save arrays and reuse them after they are done with them.

+3
source

Arrays.copyOf uses memcopy as instructions under the hood. Moreover, there are fewer indexing checks. If you assign values ​​one by one, a "bind" occurs each time.

I did not benchmark for each proposed option, but I am sure that Arrays.copyOf() will be faster than new int[] + for(...) .

On the other hand, I'm not sure that Arrays.copyOf() will be more efficient than int[] myCopy = myOrig.clone() , since cloning occurs in an array of primitive types. There is a high probability that the compiler will generate optimized bytecode. Only tests will provide a definitive answer.

+5
source

If the code belongs to you, how about the following:

 public final class ReadOnlyIntArray { private final int[] _contents; public ReadOnlyIntArray(int[] data) { _contents = (int[]) data.clone(); } public int get(int index) { return _contents[index]; } } 

Save the contents of final static in one of them and return this object instead of int[] . Make consumers clone if they are doing something destructive.

I often felt that this construct was to be one of the main constructs in java.lang, which would allow the code to return Class[] , Method[] , etc., so as not to clone every time.

0
source

Using clone() in arrays of primitive types is about 50% slower than the combination of new [] and System.arraycopy with the Java 1.6 / 1.7 x86 client virtual machine, but about the same speed with the x64 VM server.

For sizes of an array consisting of several 1000 elements or less, this is not copying, which is the most expensive part of the operation, but the creation of the array itself (i.e. creating an array of bytes of size 2048 elements takes 3 times more than before copying 2048 elements to using System.arraycopy ). This way you can get some improvement if you recycle temporary arrays.

0
source

All Articles