关于java数组拷贝的性能

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

查看其源代码:

  1. public static native void arraycopy(Object src,  int  srcPos,
  2.                                         Object dest, int destPos,
  3.                                         int length);

可以看到被定义为native方法...速度比自己写的普通方法要快.

 

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

其源代码:

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

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

别为做的测试:

  1. /*******************************************************************************
  2.  *
  3.  * 比较赋值与System.arraycopy谁快
  4.  *
  5.  * 复制的内容越多,System.arraycopy优势更明显
  6.  *
  7.  * Author: Java
  8.  *
  9.  * Modified: 2007.09.16
  10.  *
  11.  ******************************************************************************/
  12. public final class WhoFaster
  13. {
  14.   public static void main( String[] args )
  15.   {
  16.     /*/
  17.     int begin=100; 
  18.     int length=12; 
  19.     String temp="12345678901234567890"
  20.                +"12345678901234567890"
  21.                +"12345678901234567890"
  22.                +"12345678901234567890"
  23.                +"12345678901234567890"
  24.                +"黑客帝国忍者神龟变形金刚"
  25.                +"12345678901234567890"
  26.                +"12345678901234567890"
  27.                +"12345678901234567890"
  28.                +"12345678901234567890"
  29.                +"12345678901234567890"; 
  30.     int times=10000000;  //千万
  31.     /*/
  32.     int begin=100
  33.     int length=120
  34.     String temp="12345678901234567890"
  35.                +"12345678901234567890"
  36.                +"12345678901234567890"
  37.                +"12345678901234567890"
  38.                +"12345678901234567890"
  39.                +"黑客帝国忍者神龟变形金刚"
  40.                +"黑客帝国忍者神龟变形金刚"
  41.                +"黑客帝国忍者神龟变形金刚"
  42.                +"黑客帝国忍者神龟变形金刚"
  43.                +"黑客帝国忍者神龟变形金刚"
  44.                +"黑客帝国忍者神龟变形金刚"
  45.                +"黑客帝国忍者神龟变形金刚"
  46.                +"黑客帝国忍者神龟变形金刚"
  47.                +"黑客帝国忍者神龟变形金刚"
  48.                +"黑客帝国忍者神龟变形金刚"
  49.                +"12345678901234567890"
  50.                +"12345678901234567890"
  51.                +"12345678901234567890"
  52.                +"12345678901234567890"
  53.                +"12345678901234567890"
  54.     int times=1000000;  //百万
  55.     //*/ 
  56.     char[] oldArray=temp.toCharArray(); 
  57.     char[] newArray=null
  58.     long start=0L; 
  59.     ////////////////////////////////////////////////////////////////////////////
  60.     //
  61.     // 单纯赋值
  62.     //
  63.     ////////////////////////////////////////////////////////////////////////////
  64.     newArray=new char[length]; 
  65.     start=System.currentTimeMillis(); 
  66.     forint i=0; i<times; i++ )
  67.        {
  68.          forint j=0; j<length; j++ )
  69.             {
  70.               newArray[j]=oldArray[begin+j];
  71.             }
  72.        } 
  73.     System.out.println( new String( newArray )+" "+( System.currentTimeMillis()-start ) ); 
  74.     ////////////////////////////////////////////////////////////////////////////
  75.     //
  76.     // System.arraycopy
  77.     //
  78.     ////////////////////////////////////////////////////////////////////////////
  79.     newArray=new char[length]; 
  80.     start=System.currentTimeMillis(); 
  81.     forint i=0; i<times; i++ )
  82.        {
  83.          System.arraycopy( oldArray, begin, newArray, 0, length );
  84.        } 
  85.     System.out.println( new String( newArray )+" "+( System.currentTimeMillis()-start ) );
  86.   }

其结论:

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

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

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

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

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

原文地址:https://www.cnblogs.com/interdrp/p/1857083.html