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 閱讀(3123) 評論(3)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 秭归县| 兴海县| 景德镇市| 麟游县| 闵行区| 沈阳市| 水富县| 伊春市| 周宁县| 邛崃市| 天津市| 五台县| 光泽县| 神农架林区| 云龙县| 神木县| 蒙山县| 长沙市| 嘉荫县| 成安县| 精河县| 田东县| 通河县| 临安市| 武威市| 汶上县| 深州市| 平原县| 平乡县| 茌平县| 大港区| 崇明县| 公安县| 若尔盖县| 北票市| 平江县| 郧西县| 四会市| 蒲城县| 华阴市| 万盛区|