posts - 41,  comments - 40,  trackbacks - 0

          /*******************************************************************************
          ?*
          ?* 乘除法和等效的位運算
          ?*
          ?* 1024 = 2*2*2*2*2*2*2*2*2*2 = 100 0000 0000
          ?*
          ?******************************************************************************/
          class BitOperation
          {
          ? public static void main( String[] args )
          ? {
          ??? //被除數
          ??? long dividend=182495073L;

          ??? long temp=0L;

          ??? long begin=0L;


          ??? ////////////////////////////////////////////////////////////////////////////
          ??? //
          ??? // dividend/1024等效于dividend>>10
          ??? //
          ??? ////////////////////////////////////////////////////////////////////////////
          ??? begin=System.currentTimeMillis();

          ??? for( int i=0; i<10000000; i++ )
          ?????? {
          ???????? temp=dividend/1024;
          ?????? }

          ??? System.out.println( temp+" 總共耗時:"+( System.currentTimeMillis()-begin )+"毫秒" );

          ??? begin=System.currentTimeMillis();

          ??? for( int i=0; i<10000000; i++ )
          ?????? {
          ???????? temp=dividend>>10;
          ?????? }

          ??? System.out.println( temp+" 總共耗時:"+( System.currentTimeMillis()-begin )+"毫秒\r\n" );


          ??? ////////////////////////////////////////////////////////////////////////////
          ??? //
          ??? // dividend*1024等效于dividend<<10
          ??? //
          ??? ////////////////////////////////////////////////////////////////////////////
          ??? begin=System.currentTimeMillis();

          ??? for( int i=0; i<10000000; i++ )
          ?????? {
          ???????? temp=dividend*1024;
          ?????? }

          ??? System.out.println( temp+" 總共耗時:"+( System.currentTimeMillis()-begin )+"毫秒" );

          ??? begin=System.currentTimeMillis();

          ??? for( int i=0; i<10000000; i++ )
          ?????? {
          ???????? temp=dividend<<10;
          ?????? }

          ??? System.out.println( temp+" 總共耗時:"+( System.currentTimeMillis()-begin )+"毫秒\r\n" );


          ??? ////////////////////////////////////////////////////////////////////////////
          ??? //
          ??? // dividend%1024等效于dividend-( dividend>>10<<10 )
          ??? //
          ??? ////////////////////////////////////////////////////////////////////////////
          ??? begin=System.currentTimeMillis();

          ??? for( int i=0; i<10000000; i++ )
          ?????? {
          ???????? temp=dividend%1024;
          ?????? }

          ??? System.out.println( temp+" 總共耗時:"+( System.currentTimeMillis()-begin )+"毫秒" );

          ??? begin=System.currentTimeMillis();

          ??? for( int i=0; i<10000000; i++ )
          ?????? {
          ???????? temp=dividend-( dividend>>10<<10 );
          ?????? }

          ??? System.out.println( temp+" 總共耗時:"+( System.currentTimeMillis()-begin )+"毫秒\r\n" );
          ? }
          }

          posted on 2007-09-17 09:17 NeedJava 閱讀(3117) 評論(3)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 郎溪县| 永昌县| 仁化县| 洛扎县| 垫江县| 白朗县| 乌海市| 阜城县| 乡宁县| 甘肃省| 博白县| 辽中县| 米易县| 多伦县| 清镇市| 玉环县| 青海省| 京山县| 宝鸡市| 五常市| 汉寿县| 百色市| 万盛区| 丰原市| 乌海市| 阜城县| 永宁县| 凌海市| 新河县| 哈密市| 印江| 武威市| 兴海县| 彭阳县| 青海省| 北海市| 安图县| 余庆县| 南昌市| 磐安县| 汉源县|