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.

Recent Comments
Jules on Android Development – Preferences
Also, I like your wooden Gnome. He is cuteJules on Android Development – Preferences
Hey Avi, Your article was very helpful. I was trying to find something in the API to get a TextView to...mona on Memory Game – Android Application
i tried to run the app on blackberry..mona on Memory Game – Android Application
I tried this app..tis app is really cool....but when i run the game ,the spinner option is appearing even i...dante on Weekend project: Bluetooth multisender
how i send image to the devices from java using bluecove?????, this is part of may final proyect please give...Karol on Android – Multithreading in a UI environment
Hi All the examples I see so far implement the Runnable object as inner class of the main...faisal on Memory Game – Android Application
Hi, You have done excellent job. Very nice. Though I tried to build it but it did not build. Could you please...Avi on How to use fiddler+firefox to download files from streaming sites.
Try searching "Mpeg layer-3" instead.ashi on How to use fiddler+firefox to download files from streaming sites.
hi, i tried above i downloaded fiddler2 and also the script editor... also pasted the code but but i have problem in step...Rui Pedrosa on Android – Multithreading in a UI environment
A simple but very useful explanation! thanks!