Clone Vs System.arraycopy Vs looping, so which ones best?
This is the second part of the benchmark I did for copying array elements. You can read the first part here. But the readers of the post requested to include the clone method in the benchmark so here it is. The code for becnhmarking is as follows:
public class ArrayCopy {
public static final int ITERATION_AMOUNT = 150000;
public static void main(String[] args) {
int testSizes[] = { 10, 100, 1000, 10000, };
for (int i = 0; i < testSizes.length; i++)
performTest(testSizes[i]);
}
public static void performTest(int size) {
int[] sourceArray;
int[] destArray;
long startTime;
long endTime;
System.out.println("Testing array copy vs. for loop with " + size
+ " elements...");
sourceArray = new int[size];
destArray = new int[size];
// Fill the source array with garbage
for (int i = 0; i < size; i++)
sourceArray[i] = i;
/////////////////// System.arraycopy test //////////////////
// Time to initialise the timers
startTime = System.currentTimeMillis();
for (int i = 0; i < ITERATION_AMOUNT; i++)
System.arraycopy(sourceArray, 0, destArray, 0, size);
endTime = System.currentTimeMillis();
System.out.println("Copying with System.arraycopy took "
+ (endTime - startTime) + " ms.");
/////////////////////// For loop test //////////////////////
// Time to initialise the timers
startTime = System.currentTimeMillis();
for (int i = 0; i < ITERATION_AMOUNT; i++)
for (int j = 0; j < size; j++)
destArray[j] = sourceArray[j];
endTime = System.currentTimeMillis();
System.out.println("Copying with for loop took "
+ (endTime - startTime) + " ms.");
/////////////////////// For clone test //////////////////////
// Time to initialise the timers
startTime = System.currentTimeMillis();
for (int i = 0; i < ITERATION_AMOUNT; i++)
destArray = sourceArray.clone();
endTime = System.currentTimeMillis();
System.out.println("Cloning took "
+ (endTime - startTime) + " ms.\n\n");
}
}
And here are the results.
Testing array copy vs. for loop with 10 elements…
Copying with System.arraycopy took 31 ms.
Copying with for loop took 110 ms.
Cloning took 140 ms.
Testing array copy vs. for loop with 100 elements…
Copying with System.arraycopy took 47 ms.
Copying with for loop took 625 ms.
Cloning took 203 ms.
Testing array copy vs. for loop with 1000 elements…
Copying with System.arraycopy took 266 ms.
Copying with for loop took 5828 ms.
Cloning took 1703 ms.
Testing array copy vs. for loop with 10000 elements…
Copying with System.arraycopy took 1719 ms.
Copying with for loop took 63734 ms.
Cloning took 19563 ms.
And the accompanying graph to display the results graphically:

Please note that I have used the -Xint option so that there is no JIT/HotSpot compilation involved and we get the clear results.
As can be clearly seen that system.arraycopy outcalsses the clone method as well as the looping strategy. Theres is one point ot note here also that the clone method only creates a shallow copy of the array. This might not be that significant in case of copying arrays of primitive types but you can get in to a lot of trouble if the array is composed oif elements which contain some elements which are passed by reference. For more details on this topic please wait for my follow-up post for a detailed discussion on this topic.
Environment Details:
Windows XP – Service Pack 2
Intel Pentium 4 processor – 2.53 GHz
1 GB Ram
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)
About this entry
You’re currently reading “Clone Vs System.arraycopy Vs looping, so which ones best?,” an entry on My Blog - My Thoughts
- Published:
- December 29, 2007 / 6:46 pm
- Category:
- Java
- Tags:
- array copy, clone, Java, Java Performance


5 Comments
Jump to comment form | comment rss [?] | trackback uri [?]