I'm staring at a profile where one of the CPU hot-spots is a function that consists of cloning a final static int[]. You might ask, 'Why?' the callers use the results as the starting points of a hashing process.
In other words, the code needs to do (logically):
Before I either give up or start wrting microbenchmarks, I'm posting this question in case anyone has any special knowledge of the what's under the hood with clone() versus Arrays.copyOf versus just new and an arraycopy.
Arrays.copyOf
takes advantage of memcopy like instructions under the hood. Moreover, there are less index-out-of-bound checks. If you assign values one-by-one, there is a 'bound' check each time.
I have not done the benchmarking for each suggested options, but I am sure that Arrays.copyOf()
is going to be faster than new int[] + for(...)
.
On the other side, I am not sure that Arrays.copyOf()
is going to be more efficient than int[] myCopy = myOrig.clone()
, since the cloning happens on a primitive type array. There is a high probability that the compiler will generate optimized bytecode. Only tests will provide a definitive answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With