| JAVA世紀網 | 老紫竹的家 | 老紫竹的專欄 | 老紫竹的博客園 | 老紫竹的BlogJava |
          隨筆 - 3  文章 - 4  trackbacks - 0
          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           1 /**
           2  * 測試For里變量對性能的影響;
           3  *
           4  * @author 老紫竹(laozizhu.com)
           5  */
           6 public class TestFor {
           7   static int num = 100000000;
           8 
           9   public static void main(String[] args) {
          10 
          11     long begin = System.currentTimeMillis();
          12     for (int i = 0; i < num; i++) {
          13 
          14     }
          15     long end = System.currentTimeMillis();
          16     System.out.println(end - begin);
          17 
          18     // 測試間接的屬性
          19     // 影響很大
          20     A a = new A();
          21     begin = System.currentTimeMillis();
          22     for (int i = 0; i < a.b.c.d.length; i++) {
          23 
          24     }
          25     end = System.currentTimeMillis();
          26     System.out.println(end - begin);
          27 
          28     // 測試直接的屬性
          29     // 無影響
          30     D d = new D();
          31     begin = System.currentTimeMillis();
          32     for (int i = 0; i < d.length; i++) {
          33 
          34     }
          35     end = System.currentTimeMillis();
          36     System.out.println(end - begin);
          37   }
          38 
          39   static class A {
          40     B b = new B();
          41   }
          42 
          43   static class B {
          44     C c = new C();
          45   }
          46 
          47   static class C {
          48     D d = new D();
          49   }
          50 
          51   static class D {
          52     int length = num;
          53   }
          54 }


          運行結果
          78
          344
          78


          可見,如果只是簡單的 a.length, 那么與 len = a.length 直接用len沒有區別
          但如果是多級,則有較大的影響了。
          posted on 2009-03-27 07:20 老紫竹 閱讀(1692) 評論(3)  編輯  收藏

          FeedBack:
          # re: 關于for里面使用變量對性能的影響 2009-03-27 10:22 測試
          因為在這句之前:for (int i = 0; i < a.b.c.d.length; i++) {
          虛擬機已經將類A加載,類B,類C,類D都還沒加載,只有在調用a.b,進行符號解析的時候才會加載類B,其它同理
          類加載完還有初始化過程,加載,連接,初始化,符號解析都要花費時間  回復  更多評論
            
          # re: 關于for里面使用變量對性能的影響 2009-03-27 10:45 yesman
          從字節碼上看
          情況一是1個getstatic num
          二是aload a;然后是4個getfield
          三是aload d;然后1個getfield

          再看結果,1,3情況差不多,說明getstatic和getfield的性能是差不多的,2的話執行了4次,自然要慢2-4倍,結果也驗證了這點  回復  更多評論
            
          # re: 關于for里面使用變量對性能的影響 2009-03-27 12:31 Johnny Jian
          這樣的測試很有問題,因為JVM都還沒有warmup夠
          建議你看看這篇文章:http://www.ibm.com/developerworks/cn/java/j-benchmark1.html?ca=drs-cn-0709  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 绩溪县| 哈密市| 霍州市| 依安县| 浙江省| 库尔勒市| 望城县| 白银市| 永安市| 贞丰县| 南江县| 阿坝县| 丹凤县| 石屏县| 台南市| 海原县| 毕节市| 台江县| 宁化县| 东光县| 合阳县| 黔江区| 礼泉县| 乳山市| 高清| 东兰县| 通化县| 苗栗县| 济南市| 隆昌县| 扎赉特旗| 红原县| 黔东| 富阳市| 新密市| 奉节县| 镇巴县| 楚雄市| 九龙县| 平塘县| 绵竹市|