System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) is a native method.
What is the time complexity for this method?
I did some investigation and later decided to write a test code, here is what I have.
My testing code is given below:
import org.junit.Test;
public class ArrayCopyTest {
@Test
public void testCopy() {
for (int count = 0; count < 3; count++) {
int size = 0x00ffffff;
long start, end;
Integer[] integers = new Integer[size];
Integer[] loopCopy = new Integer[size];
Integer[] systemCopy = new Integer[size];
for (int i = 0; i < size; i++) {
integers[i] = i;
}
start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
loopCopy[i] = integers[i];
}
end = System.currentTimeMillis();
System.out.println("for loop: " + (end - start));
start = System.currentTimeMillis();
System.arraycopy(integers, 0, systemCopy, 0, size);
end = System.currentTimeMillis();
System.out.println("System.arrayCopy: " + (end - start));
}
}
}
It produces result shown below
for loop: 47
System.arrayCopy: 24
for loop: 31
System.arrayCopy: 22
for loop: 36
System.arrayCopy: 22
So, Bragboy is correct.