A colleague in the group always uses this system. arraycopy (), I don't understand it, and then I checked it, and then I knew how happy people who are familiar with the bottom layer are, and what function should I use if I want to use... My Wordpress blog:
Http://www.naitiz.com/index.php/android-quick-tip-use-system-arraycopy_125.html
This article is a translation, the original address:
Http://www.aviyehuda.com/2011/06/android-quick-tip-use-system-arraycopy/
As we all know, using JNISystem. arraycopy ()
It is an effective method for copying arrays because it uses native to call memory. But is this also applicable to Android platforms? If so, to what extent is it more effective?
To answer this question, I made a simple test and ran it in the activity of PC and Android.
Below is the test on PCCode:
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 );
}
PC testing results: (Java 7, 8 GB memory, CPU intel I5)
Naive algorithm-14 MS
System. arraycopy ();-6 ms.
Arraycopy takes half the time.
In the same test on Android, the Code is as follows:
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 );
}
}
*Note that I will change the length of the array from1000 millionTo100 million
This is becauseAndroidThe memory allowed on the platform is limited.
InNexus 1
The running result is:
Naive algorithm-182 MS
System. arraycopy ();-12 ms.
This fact means thatAndroidUse onSystem. arraycopy ()It is much faster than copying an ordinary array.
In general, try to use it on AndroidSystem. arraycopy ()To replace array replication.