Loop VS System.arraycopy


Everyone feels the need to copy arrays around in java. So whats the best option to use?

  1. Using a simple for loop to iterate and copy the content of each element over to the other element.
  2. Using System.arraycopy to copy over the contents

Lets have a faceoff of these two methods and get the numbers out to prove the point. Here is the benchmarking code I wrote to get the numbers.

public class ArrayCopy {
public static final int ITERATION_AMOUNT = 150000;

public static void main(String[] args) {
int testSizes[] = { 10, 100, 1000, 10000, 100000, 1000000 };
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.\n\n"); } }[/sourcecode] And here are the results… Testing array copy vs. for loop with 10 elements…
Copying with System.arraycopy took 15 ms.
Copying with for loop took 16 ms.

Testing array copy vs. for loop with 100 elements…
Copying with System.arraycopy took 15 ms.
Copying with for loop took 79 ms.

Testing array copy vs. for loop with 1000 elements…
Copying with System.arraycopy took 187 ms.
Copying with for loop took 625 ms.

Testing array copy vs. for loop with 10000 elements…
Copying with System.arraycopy took 2156 ms.
Copying with for loop took 4625 ms.

I managed to generate a chart to display the results graphiacally.
Arraycopy

Since System.arraycopy is a native method it is going to be the faster than the looping approach. I think this should be enough to convince our CTO that System.arraycopy is indeed faster than the looping approach that he seems to utilize in one of the most critical sections of code assuming that he is writing the fastest piece of code.

Advertisements
Loop VS System.arraycopy

8 thoughts on “Loop VS System.arraycopy

  1. @Ron – I will be adding clone to the benchmark.

    @Ashkan – I used XL for generating charts but if you wan tto generate them programatically then you can use JFreeChart or try out the Google Chart API. Very cool APIs.

  2. Hmm, word press issues, forgive me if this post is a duplicate.
    Hi, I was reading this as well as the follow up 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 of your figures, divide by 150,000
    and then you’ve got it.

  3. I cared enough to rerun the code on a modern Java 7.

    Testing array copy vs. for loop with 10 elements…
    Copying with System.arraycopy took 33 ms.
    Copying with for loop took 45 ms.

    Testing array copy vs. for loop with 100 elements…
    Copying with System.arraycopy took 8 ms.
    Copying with for loop took 16 ms.

    Testing array copy vs. for loop with 1000 elements…
    Copying with System.arraycopy took 97 ms.
    Copying with for loop took 145 ms.

    Testing array copy vs. for loop with 10000 elements…
    Copying with System.arraycopy took 1583 ms.
    Copying with for loop took 1584 ms.

    Testing array copy vs. for loop with 100000 elements…
    Copying with System.arraycopy took 46101 ms.
    Copying with for loop took 47080 ms.

    That’s right, baby… the compiler replaced the loop with the System.arraycopy natively. And then the Netbeans IDE flagged the loop with a note that it should be replaced with System.arraycopy. You are no longer allowed to do it wrong.

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