Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Android quick tip: use System.arraycopy()

Posted on 25/06/2011

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.

Test for PC
Test for Android

2 thoughts on “Android quick tip: use System.arraycopy()”

  1. dd says:
    08/07/2012 at 16:54

    Thanks a lot! This spared me the effort of trying for myself.
    Regards, Daniel

    Reply
  2. Mert says:
    28/04/2013 at 15:07

    Thanks a lot. Saved some amount of times.
    Regards, Mert.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
1d96f52e7159fe09c7a3dd2a9816d166-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme