java数组拷贝

java 提供的System.arrayCopy()方法比自己写的数组copy要快.

查看其源代码:

public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);
   src - 源数组。
    srcPos - 源数组中的起始位置。
    dest - 目标数组。
    destPos - 目标数据中的起始位置。
    length - 要复制的数组元素的数量。
可以看到被定义为native方法...速度比自己写的普通方法要快.



在jdk1.6中加入了新的数组拷贝方法.Arrays.copyOfRange().

其源代码:

 public static byte[] copyOfRange(byte[] original, int from, int to) { 
        int newLength = to - from; 
        if (newLength < 0) 
            throw new IllegalArgumentException(from + " > " + to); 
        byte[] copy = new byte[newLength]; 
        System.arraycopy(original, from, copy, 0, 
                         Math.min(original.length - from, newLength)); 
        return copy; 
    }



其实就是System.arraycopy..晕死.



别为做的测试:

/******************************************************************************* 
 * 
 * 比较赋值与System.arraycopy谁快 
 * 
 * 复制的内容越多,System.arraycopy优势更明显 
 * 
 * Author: NeedJava 
 * 
 * Modified: 2007.09.16 
 * 
 ******************************************************************************/ 
public final class WhoFaster 
{ 
  public static void main( String[] args ) 
  { 
    /*/ 
    int begin=100;  

    int length=12;  

    String temp="12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"黑客帝国忍者神龟变形金刚" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890";  

    int times=10000000;  //千万 
    /*/ 
    int begin=100;  

    int length=120;  

    String temp="12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"黑客帝国忍者神龟变形金刚" 
               +"黑客帝国忍者神龟变形金刚" 
               +"黑客帝国忍者神龟变形金刚" 
               +"黑客帝国忍者神龟变形金刚" 
               +"黑客帝国忍者神龟变形金刚" 
               +"黑客帝国忍者神龟变形金刚" 
               +"黑客帝国忍者神龟变形金刚" 
               +"黑客帝国忍者神龟变形金刚" 
               +"黑客帝国忍者神龟变形金刚" 
               +"黑客帝国忍者神龟变形金刚" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890" 
               +"12345678901234567890";  

    int times=1000000;  //百万 
    //*/  

    char[] oldArray=temp.toCharArray();  

    char[] newArray=null;  

    long start=0L;  


    //////////////////////////////////////////////////////////////////////////// 
    // 
    // 单纯赋值 
    // 
    //////////////////////////////////////////////////////////////////////////// 
    newArray=new char[length];  

    start=System.currentTimeMillis();  

    for( int i=0; i<times; i++ ) 
       { 
         for( int j=0; j<length; j++ ) 
            { 
              newArray[j]=oldArray[begin+j]; 
            } 
       }  

    System.out.println( new String( newArray )+" "+( System.currentTimeMillis()-start ) );  


    //////////////////////////////////////////////////////////////////////////// 
    // 
    // System.arraycopy 
    // 
    //////////////////////////////////////////////////////////////////////////// 
    newArray=new char[length];  

    start=System.currentTimeMillis();  

    for( int i=0; i<times; i++ ) 
       { 
         System.arraycopy( oldArray, begin, newArray, 0, length ); 
       }  

    System.out.println( new String( newArray )+" "+( System.currentTimeMillis()-start ) ); 
  } 
}  




其结论:

在第一种情况,循环千万,一个900,一个1000,两者相差100毫秒

第二种情况就拉大了,循环千万,一个6700,一个2200,相差4500毫秒

为防止JVM将字符数组作为常量保存在内存中,我分别屏蔽运行,效果一样。

也就是说,对于很短的字符串复制,单纯赋值略快,可以忽略

对于很长的字符串复制,用单纯赋值就是脏代码

转自:http://luoyahu.iteye.com/blog/365465

你可能感兴趣的:(java,jvm,Blog,J#)