Well it’s a well known fact that the java native function System.arraycopy() is a useful way to copy one array to another since it is native, but is that also the case for Android? And if so, how much more useful is it?
To answer these questions I have made a simple test and ran it as a java program on my PC and than as an Android activity.
Here is the test for the PC:
private static final int SIZE_OF_ARRAY = 10000000; private static long time; public static void main(String[] args) { Integer [] sourceArray = new Integer[SIZE_OF_ARRAY]; Integer [] destinationArray = new Integer[SIZE_OF_ARRAY]; fillArray(sourceArray); startBenchmark(); naiveCopy(sourceArray,destinationArray); stopBenchmark(); startBenchmark(); System.arraycopy(sourceArray, 0, destinationArray, 0, sourceArray.length); stopBenchmark(); } private static void naiveCopy(Integer [] src, Integer [] dst) { for (int i = 0; i < src.length; i++) { dst[i]=src[i]; } } private static void fillArray(Integer [] src) { for (int i = 0; i < src.length; i++) { src[i]=i; } } private static void startBenchmark() { time = System.currentTimeMillis(); } private static void stopBenchmark() { time = System.currentTimeMillis() - time; System.out.println( "time="+time); }
Here are the results while running it from my PC (java 7, 8GB memory, CPU intel i5):
Naive algorithm – 14 ms
System.arraycopy(); – 6 ms.
Arraycopy does the task in less than half of the time.
Now to use it on Android – here is the code:
public class ArrayCopyTestActivity extends Activity { private static final int SIZE_OF_ARRAY = 1000000; private long time; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Integer [] sourceArray = new Integer[SIZE_OF_ARRAY]; Integer [] dst = new Integer[SIZE_OF_ARRAY]; fillArray(sourceArray); startBenchmark(); naiveCopy(sourceArray,dst); stopBenchmark(); startBenchmark(); System.arraycopy(sourceArray, 0, dst, 0, sourceArray.length); stopBenchmark(); } private void naiveCopy(Integer [] src, Integer [] dst) { for (int i = 0; i < src.length; i++) { dst[i]=src[i]; } } private void fillArray(Integer [] src) { for (int i = 0; i < src.length; i++) { src[i]=i; } } private void startBenchmark() { time = System.currentTimeMillis(); } private void stopBenchmark() { time = System.currentTimeMillis() - time; Log.d("array copy test", "time="+time); } }
* Notice I have reduced the size of the Array from 10 million to 1 million, this is due to restrictions on memory for applications in Android.
The results from running it on my device (nexus 1):
Naive algorithm – 182 ms
System.arraycopy(); – 12 ms.
This means that the fact that System.arraycopy() is better than the regular copy is even more true for Android.
In short than, always use System.arraycopy() especially on Android.
Thanks a lot! This spared me the effort of trying for myself.
Regards, Daniel
Thanks a lot. Saved some amount of times.
Regards, Mert.