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[][]?

3
  • 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? Commented Oct 12, 2011 at 16:57
  • 2
    Why not add arr.clone() to your list, benchmark all four methods, and post the results here? :-) Commented Oct 12, 2011 at 16:57
  • @aix, clone() is the same as Arrays.copy when JIT'd Commented Dec 15, 2011 at 10:45

5 Answers 5

14

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().

Sign up to request clarification or add additional context in comments.

7 Comments

interesting, the same site says avoid clone... javapractices.com/topic/TopicAction.do?Id=71
This is the kind of answer I like to see! :)
@Simon the site says "avoid implementing clone" but not to avoid clone
Why there is so difference between System.arraycopy() (110 ms) and Arrays.copyOf() (187 ms) ? Is not Arrays.copyOf() uses System.arraycopy() internally ?
Their benchmark is significantly flawed. I would not trust their results (which BTW I know are inaccurate).
|
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.

Comments

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.

Comments

1

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

Here is the corresponding bug: https://bugs.java.com/bugdatabase/view_bug?bug_id=6428387

Comments

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; } 

1 Comment

this is irrelevant actually, it uses intrinsics, the code is not executed by the JIT like that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.