Comparison of efficiency of copy array method in four

Source: Internet
Author: User

JavainCopyThere are four ways to array. The first type isObjectclass provides theClone ()method, the second type isJava.lang.Systemclass provides thearrayCopy ()method, and the third type isjava.util.Arraysclass provides thecopyOf ()method, the last one is the most common use forLoop implements the array'sCopy. The four-way execution method has different execution efficiency because of the internal implementation. Refer to the running results of the following code (using dynamic proxies because of the same business logic)

Proxy class:

package com.zly.test;import  java.lang.reflect.invocationhandler;import java.lang.reflect.method;public class agent  implements invocationhandler{private arraycopymethodcompareinterface acmc1= new  ArrayCopyMethodCompare1 ();  PUBLIC ARRAYCOPYMETHODCOMPAREINTERFACE GETACMC1 ()  {return  ACMC1;} PUBLIC VOID SETACMC1 (ARRAYCOPYMETHODCOMPAREINTERFACE ACMC1)  {THIS.ACMC1 = ACMC1;} @Overridepublic  object invoke (Object proxy, method method, object[] args) Throws throwable {long starttime = system.currenttimemillis (); Method.invoke (ACMC1,  args); Long endtime = system.currenttimemillis (); System.out.println (endtime-starttime); return null;}} 

Agent interface:

Package Com.zly.test;import Java.lang.reflect.proxy;import Java.util.arrays;public interface arraycopymethodcompareinterface {public void system_arraycopy ();p ublic void For_copy ();p ublic void Clone_copy (); public void arrays_copyof ();}

The Proxy interface implementation class:

Package com.zly.test;import java.lang.reflect.proxy;import java.util.arrays;public class  arraycopymethodcompare1 implements arraycopymethodcompareinterface{public static int [] a = new int[2000000];p ublic int[] b = new int[a.length];p ublic  static void main (String[] args)  {for (int i=0;i<a.length;i++)  {a[i]=i+1;} Arraycopymethodcompareinterface acmc = new arraycopymethodcompare1 (); Agent a = new agent (); A.SETACMC1 (ACMC); arraycopymethodcompareinterface arraycopymethodcompareproxy =  (ArrayCopyMethodCompareInterface) Proxy.newproxyinstance (Acmc.getclass (). getClassLoader (), New class[]{arraycopymethodcompareinterface.class }, a); arraycopymethodcompareproxy.clone_copy ();/*arraycopymethodcompareproxy.for_copy (); Arraycopymethodcompareproxy.system_arraycopy (); arraycopymethodcompareproxy.arrays_copyof (); */}public void system_arraycopy ()  {system.arraycopy (a, 0, b, 0, a.length); b=null;} Public void for_copy ()  {for (int i=0;i<a.length-1;i++)  {b[i]=a[i];} B=null;} Public void clone_copy ()  {b=a.clone (); b=null;} Public void arrays_copyof ()  {b=arrays.copyof (a, a.length); b=null;}}

650) this.width=650; "Width=" 695 "height=" 366 "src="/e/u261/themes/default/images/spacer.gif "style=" background : URL ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/> through the operation of the above code, although the results of each run is not stable, but the overall can draw the following conclusions:

System.arraycopy ()(7 ms)>clone () ( milliseconds) >arrays.copyof () (in milliseconds)>for cycle ( milliseconds)

This article is from the "Java" blog, so be sure to keep this source http://a001807.blog.51cto.com/9804844/1905265

Comparison of efficiency of copy array method in four

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.