程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> J2ME代碼效率測試_for循環和除法

J2ME代碼效率測試_for循環和除法

編輯:J2ME
測試環境:
  Nokia 3300真機,老40,與7210同一個系列
測試代碼:

long time1 = System.currentTimeMillis();
       int a = 45456485, b = 0;
       for(int i = 100000; --i >= 0;) {
           b = a >> 10;
       }
       tt1 = System.currentTimeMillis() - time1;//307
       
Java手機網[www.cnjm.Net]       time1 = System.currentTimeMillis();
       for(int i = 100000; --i >= 0;) {
           b = a / 1024;
       }
       tt2 = System.currentTimeMillis() - time1;//584
       
       time1 = System.currentTimeMillis();
       for(int i = 0; i < 100000; i++) {
           b = a / 1024;
       }
       tt3 = System.currentTimeMillis() - time1;//647
       
       time1 = System.currentTimeMillis();
       for(int i = 0; i < 100000; i++) {
           b = a >> 10;
       }
      tt4 = System.currentTimeMillis() - time1;//354
     
      System.out.println(String.valueOf(b));

結論:
 for循環建議寫成這樣:
   for(int i = value; --i >= 0;), 可提高效率15%左右
 除法時如果是除以2的N次方
  建議寫成 >> N, 可提高效率90%左右, 乘法同理
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved