What is the fastest way to copy double [] in Java?

A simple question is what is the fastest way to copy an array of doublings in Java. I am currently doing 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 performs the selection to avoid overflow, but if there is a faster way, I separate the selection from the copy.

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

Edit: How about copying double[][] ?

+6
source share
5 answers

Java practice has compared various copy methods in int arrays :

Here are the results from your site:

java -cp. -Xint ArrayCopier performance 250,000

Clone Usage: 93 ms
Using System.arraycopy: 110 ms
Using Array.copyOf: 187 ms
Usage for cycle: 422 ms

It seems to me that the connection is between System.arraycopy() and clone() .

+11
source

System.arraycopy is probably best 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.

+7
source

System.arraycopy () is your best bet. As a rule, it is implemented using its own instructions, possibly related to direct calls to the memory manager of the operating system.

In Java 6, arraycopy performance was improved because "manual assembly nodes are now used for each type size when there is no overlap."

Read more.

+1
source

Arrays.copy, [] .clone, and System.arraycopy with the new object use the same code path when the JIT is correct.

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

+1
source
 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; } 
0
source

Source: https://habr.com/ru/post/899148/


All Articles