7

Simple question, what's the fastest way of copying an array of doubles in Java. I currently do this...

public static double[] clone_doubles(double[] from)
{
    double[] to = new double[from.length];
    for (int i = 0; i < from.length; i++) to[i] = from[i];
    return to;
}

which also does the allocation to avoid overflows, but if there is a quicker way I will separate the allocation from the copy.

I have looked at Arrays.copyOf() and System.arraycopy() but I'm wondering if anyone has any neat tricks.

Edit: How about copying a double[][]?

Simon
  • 78,655
  • 25
  • 88
  • 118
  • 1
    What do you mean by "separate the allocation from the copy"? Do you mean that you are able to re-use destination arrays instead of allocating a new one? I'd guess that `System.arraycopy()` is fastest, much faster than your code. Have you profiled it? – erickson Oct 12 '11 at 16:57
  • 2
    Why not add `arr.clone()` to your list, benchmark all four methods, and post the results here? :-) – NPE Oct 12 '11 at 16:57
  • @aix, clone() is the same as Arrays.copy when JIT'd – bestsss Dec 15 '11 at 10:45

5 Answers5

13

Java Practices did a comparison of different copy methods on arrays of int:

Here are the results from their site:

java -cp . -Xint ArrayCopier performance 250000

Using clone: 93 ms
Using System.arraycopy: 110 ms
Using Arrays.copyOf: 187 ms
Using for loop: 422 ms

Looks to me like a tie between System.arraycopy() and clone().

Community
  • 1
  • 1
dtyler
  • 1,187
  • 6
  • 11
  • interesting, the same site says avoid clone... http://www.javapractices.com/topic/TopicAction.do?Id=71 – Simon Oct 12 '11 at 17:08
  • This is the kind of answer I like to see! :) – monksy Oct 12 '11 at 17:12
  • Since `clone()` does a shallow copy, there is a risk of references in your new array pointing back to things belonging to objects in your old array. As it says this isn't a risk for 1 dimensional primitive arrays like the one in your example. – dtyler Oct 12 '11 at 17:14
  • 1
    @Simon the site says "avoid implementing clone" but not to avoid clone – Naveen Babu Oct 12 '11 at 17:21
  • 1
    Why there is so difference between System.arraycopy() (110 ms) and Arrays.copyOf() (187 ms) ? Is not Arrays.copyOf() uses System.arraycopy() internally ? – Vasu Dec 03 '11 at 17:40
  • 1
    Their benchmark is significantly flawed. I would not trust their results (which BTW I know are inaccurate). – assylias May 29 '13 at 23:40
  • @Kailash Definitely, the results make no sense. – assylias May 29 '13 at 23:41
7

System.arraycopy is probably your best bet if you just want to copy from one array to another.

Otherwise

public static double[] clone_doubles(double[] from) {
  return (double[]) from.clone();
}

will give you a clone.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

System.arraycopy() is your best bet. Generally, it's implemented with native instructions, probably involving direct calls to the operating system's memory manager.

In Java 6 the performance of arraycopy was improved as "hand-coded assembly stubs are now used for each type size when no overlap occurs".

Read this other post for further details.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

Arrays.copy, [].clone and System.arraycopy w/ new object are using the same code path when properly JIT.

Here is the corresponding bug: http://bugs.sun.com/view_bug.do?bug_id=6428387

bestsss
  • 11,796
  • 3
  • 53
  • 63
0
public static double[] clone_doubles(double[] from)
{
    return Arrays.copyOf(from, from.length);
}

This is internally implemented using System.arrayCopy():

public static double[] copyOf(double[] original, int newLength) {
    double[] copy = new double[newLength];
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588