arraycopy

System.arrayCopy is slow

青春壹個敷衍的年華 提交于 2019-11-29 00:59:51
问题 I've been trying to measure the performance of the System.arrayCopy vs Arrays.copyOf in order to choose properly one of them. Just for the sake of benchmark I added manual copy as well and the result surprised me. Obviously I'm missing something really important, could you, please, tell me, what it is? The implementation is as follows (see first 4 methods). public class ArrayCopy { public static int[] createArray( int size ) { int[] array = new int[size]; Random r = new Random(); for ( int i

Appending a byte[] to the end of another byte[] [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-29 00:31:37
问题 This question already has an answer here: Easy way to concatenate two byte arrays 12 answers I have two byte[] arrays which are of unknown length and I simply want to append one to the end of the other, i.e.: byte[] ciphertext = blah; byte[] mac = blah; byte[] out = ciphertext + mac; I have tried using arraycopy() but can't seem to get it to work. 回答1: Using System.arraycopy(), something like the following should work: // create a destination array that is the size of the two arrays byte[]

Why is System.arraycopy native in Java?

懵懂的女人 提交于 2019-11-26 21:36:40
I was surprised to see in the Java source that System.arraycopy is a native method. Of course the reason is because it's faster. But what native tricks is the code able to employ that make it faster? Why not just loop over the original array and copy each pointer to the new array - surely this isn't that slow and cumbersome? In native code, it can be done with a single memcpy / memmove , as opposed to n distinct copy operations. The difference in performance is substantial. It can't be written in Java. Native code is able to ignore or elide the difference between arrays of Object and arrays of

Why is System.arraycopy native in Java?

浪尽此生 提交于 2019-11-26 07:59:05
问题 I was surprised to see in the Java source that System.arraycopy is a native method. Of course the reason is because it\'s faster. But what native tricks is the code able to employ that make it faster? Why not just loop over the original array and copy each pointer to the new array - surely this isn\'t that slow and cumbersome? 回答1: In native code, it can be done with a single memcpy / memmove, as opposed to n distinct copy operations. The difference in performance is substantial. 回答2: It can