java學習

          java學習

           

          java程序性能優化2

          六、避免不需要的instanceof操作 如果左邊的對象的靜態類型等于右邊的,instanceof表達式返回永遠為true。 例子: public class uiso { public uiso () {} } class dog extends uiso { void method (dog dog, uiso u) { dog d = dog;if (d instanceof uiso) // always true. system.out.println("dog is a uiso"); uiso uiso = u; if (uiso instanceof object) // always true. system.out.println("uiso is an object"); } } 更正: 刪掉不需要的instanceof操作。 class dog extends uiso { void method () { dog d; system.out.println ("dog is an uiso"); system.out.println ("uiso is an uiso"); } }
          七、避免不需要的造型操作 所有的類都是直接或者間接繼承自object。同樣,所有的子類也都隱含的“等于”其父類。那么,由子類造型至父類的操作就是不必要的了。 例子: class unc { string _id = "unc"; } class dog extends unc { void method () { dog dog = new dog (); unc animal = (unc)dog; // not necessary. object o = (object)dog; // not necessary. } } 更正: class dog extends unc { void method () { dog dog = new dog(); unc animal = dog; object o = dog; } }
          八、如果只是查找單個字符的話,用charat()代替startswith()
          用一個字符作為參數調用startswith()也會工作的很好,但從性能角度上來看,調用用string api無疑是錯誤的! 例子: public class pcts { private void method(string s) { if (s.startswith("a")) { // violation // ... } } } 更正 將'startswith()' 替換成'charat()'. public class pcts { private void method(string s) { if ('a' == s.charat(0)) { // ... } } }
          九、使用移位操作來代替'a / b'操作 "/"是一個很“昂貴”的操作,使用移位操作將會更快更有效。 例子: public class sdiv { public static final int num = 16; public void calculate(int a) { int div = a / 4; // should be replaced with "a >> 2". int div2 = a / 8; // should be replaced with "a >> 3". int temp = a / 3; } } 更正: public class sdiv { public static final int num = 16; public void calculate(int a) { int div = a >> 2; int div2 = a >> 3; int temp = a / 3; // 不能轉換成位移操作 } }
          十、使用移位操作代替'a * b'
          同上。 [i]但我個人認為,除非是在一個非常大的循環內,性能非常重要,而且你很清楚你自己在做什么,方可使用這種方法。否則提高性能所帶來的程序晚讀性的降低將是不合算的。 例子: public class smul { public void calculate(int a) { int mul = a * 4; // should be replaced with "a << 2". int mul2 = 8 * a; // should be replaced with "a << 3". int temp = a * 3; } } 更正: package opt; public class smul { public void calculate(int a) { int mul = a << 2; int mul2 = a << 3; int temp = a * 3; // 不能轉換 } }
          十一、在字符串相加的時候,使用 ' ' 代替 " ",如果該字符串只有一個字符的話 例子: public class str { public void method(string s) { string string = s + "d" // violation. string = "abc" + "d" // violation. } } 更正: 將一個字符的字符串替換成' ' public class str { public void method(string s) { string string = s + 'd' string = "abc" + 'd' } }
          十二、將try/catch塊移出循環 把try/catch塊放入循環體內,會極大的影響性能,如果編譯jit被關閉或者你所使用的是一個不帶jit的jvm,性能會將下降21%之多! 例子: import java.io.fileinputstream; public class try { void method (fileinputstream fis) { for (int i = 0; i < size; i++) { try { // violation _sum += fis.read(); } catch (exception e) {} } } private int _sum; } 更正: 將try/catch塊移出循環 void method (fileinputstream fis) { try { for (int i = 0; i < size; i++) {
          _sum += fis.read(); } } catch (exception e) {} }
          十三、對于boolean值,避免不必要的等式判斷 將一個boolean值與一個true比較是一個恒等操作(直接返回該boolean變量的值). 移走對于boolean的不必要操作至少會帶來2個好處: 1)代碼執行的更快 (生成的字節碼少了5個字節); 2)代碼也會更加干凈 。 例子: public class ueq { boolean method (string string) { return string.endswith ("a") == true; // violation } } 更正: class ueq_fixed { boolean method (string string) { return string.endswith ("a"); } }
          十四、對于常量字符串,用'string' 代替 'stringbuffer' 常量字符串并不需要動態改變長度。 例子: public class usc { string method () { stringbuffer s = new stringbuffer ("hello"); string t = s + "world!"; return t; } } 更正: 把stringbuffer換成string,如果確定這個string不會再變的話,這將會減少運行開銷提高性能。
          十五、使用條件操作符替代"if (cond) return; else return;" 結構 條件操作符更加的簡捷 例子: public class if { public int method(boolean isdone) { if (isdone) { return 0; } else { return 10; } } } 更正: public class if { public int method(boolean isdone) { return (isdone ? 0 : 10); } }
          十六、不要在循環體中實例化變量 在循環體中實例化臨時變量將會增加內存消耗 例子: import java.util.vector; public class loop { void method (vector v) { for (int i=0;i < v.size();i++) { object o = new object(); o = v.elementat(i); } } } 更正: 在循環體外定義變量,并反復使用 import java.util.vector; public class loop { void method (vector v) { object o; for (int i=0;i<v.size();i++) { o = v.elementat(i); } } }

          posted on 2013-02-22 14:16 楊軍威 閱讀(200) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 贡嘎县| 新民市| 衡南县| 咸阳市| 富锦市| 大荔县| 无极县| 南城县| 陇南市| 井冈山市| 当雄县| 兴安盟| 荆门市| 三河市| 卓尼县| 托克逊县| 新巴尔虎右旗| 辽阳市| 哈尔滨市| 大悟县| 马鞍山市| 庄河市| 东乡族自治县| 东阿县| 荥阳市| 和林格尔县| 鲁山县| 黑龙江省| 张家口市| 洛南县| 阿鲁科尔沁旗| 婺源县| 蒲城县| 全椒县| 大理市| 逊克县| 阳谷县| 宁强县| 商水县| 日喀则市| 文安县|