/*******************************************************************************
?*
?* 比較賦值與System.arraycopy誰(shuí)快
?*
?* 復(fù)制的內(nèi)容越多,System.arraycopy優(yōu)勢(shì)更明顯
?*
?* 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"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890";
??? int times=10000000;? //千萬(wàn)
??? /*/
??? int begin=100;
??? int length=120;
??? String temp="12345678901234567890"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"黑客帝國(guó)忍者神龜變形金剛"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890"
?????????????? +"12345678901234567890";
??? int times=1000000;? //百萬(wàn)
??? //*/
??? 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 閱讀(5094)
評(píng)論(8) 編輯 收藏 所屬分類:
Java