System. arraycopy () Analysis

Source: Internet
Author: User
Tags wordpress blog
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.