Assuming a 10 element or less Object[] in Java, what would be the fastest way of copying the array?
-
for(int i = 0;i < a.length;i++) -
for(int i = 0,l = a.length;i < l;i++) // i.e. is caching array len in local var faster? -
System.arrayCopy(a, 0, a2, 0, a.length);
System.arraycopy() is the fastest way to copy array — as it designed and optimized exactly for this job. There was rumors that for small arrays it hadcoded loop may be faster — but it is not true for now. System.arraycopy is a JIT intrinsics, and JIT choose best implementation for each case.