DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Managing Data Residency, the Demo
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
  • Front-End: Cache Strategies You Should Know
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends

Trending

  • Managing Data Residency, the Demo
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
  • Front-End: Cache Strategies You Should Know
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Android quick tip: use System.arraycopy()

Android quick tip: use System.arraycopy()

Avi Yehuda user avatar by
Avi Yehuda
·
Jun. 29, 11 · News
Like (0)
Save
Tweet
Share
3.70K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

Android (robot)

Published at DZone with permission of Avi Yehuda, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Managing Data Residency, the Demo
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
  • Front-End: Cache Strategies You Should Know
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: