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"); } } [/sourcecode]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:

clone.gif
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)

Advertisements
Clone Vs System.arraycopy Vs looping, so which ones best?

6 thoughts on “Clone Vs System.arraycopy Vs looping, so which ones best?

  1. faisalferoz says:

    @Ron here are the results with -Xcomp

    Testing array copy vs. for loop with 10 elements
    Copying with System.arraycopy took 16 ms.
    Copying with for loop took 156 ms.
    Cloning took 78 ms.

    Testing array copy vs. for loop with 100 elements…
    Copying with System.arraycopy took 31 ms.
    Copying with for loop took 63 ms.
    Cloning took 172 ms.

    Testing array copy vs. for loop with 1000 elements…
    Copying with System.arraycopy took 203 ms.
    Copying with for loop took 515 ms.
    Cloning took 2282 ms.

    Testing array copy vs. for loop with 10000 elements…
    Copying with System.arraycopy took 2718 ms.
    Copying with for loop took 6422 ms.
    Cloning took 21281 ms.

    Seems like precompilation messes up the clone. I searched on the subject and people ususally suggest its not a good idea to use this option in production in general as the -Xcomp flag is more a stress mode for the VM implementors. Using it causes premature compilation and unnecessary recompilations later.

  2. Sam says:

    Hi, I was reading this as well as the post before and was quite surprised by the results you showed. For example, your claim that it took 2.7 seconds to copy an array of 10000 elements… I mean that’s super slow by any remotely modern standard. So I looked at your code… You forgot to divide by ITERATION_AMOUNT in your print outs. Granted the proportions of your graphs are still readable and correct, but the overall post gives a confusing impression of java array performance. Basically take all your figures, divide by 150,000
    and then you’ve got it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s