posts - 41,  comments - 40,  trackbacks - 0

/*******************************************************************************
 *
 * 比较赋值与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 ) );
  }
}

posted on 2007-09-16 13:42 NeedJava 阅读(5072) 评论(8)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: