国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Java性能優(yōu)化

2019-11-14 23:24:51
字體:
供稿:網(wǎng)友
java性能優(yōu)化作者:禪樓望月(http://m.survivalescaperooms.com/yaoyinglong)

注:里面的測試結(jié)果會(huì)因電腦配置的不同而有所差異!!!

1. 為一些集合定義初始化大小

List、Set、Map都會(huì)有有一個(gè)默認(rèn)的初始化大小,但是這個(gè)值往往不夠我們使用,這時(shí)候JVM便會(huì)申請更大的一塊內(nèi)存給集合,然后將原集合中的數(shù)據(jù)復(fù)制過來,最后原集合等待被作為垃圾而回收。可見擴(kuò)容是一件比較費(fèi)事的事情,所以最好能準(zhǔn)確的估計(jì)你所需要的最佳大小。

[+]view code

publicclass PerformanceOptimization {

publicstaticvoid main(String[] args) { long start=new Date().getTime(); List<String> a=new ArrayList<String>(); for(int i=0; i<1000000; i++){ a.add("a"); } long end=new Date().getTime(); System.out.PRintln(end-start);//47 } }

優(yōu)化后:

[+]view code

publicclass PerformanceOptimization {

publicstaticvoid main(String[] args) { long start=new Date().getTime(); List<String> a=new ArrayList<String>(1000000); for(int i=0; i<1000000; i++){ a.add("a"); } long end=new Date().getTime(); System.out.println(end-start);//32 } }

這樣積少成多,效果還是挺可觀的。

2. 復(fù)制數(shù)組的時(shí)候使用System.arraycopy來代替循環(huán)copy,它的效率更高更簡潔。 [+]view code

publicclass PerformanceOptimization {

publicstaticvoid main(String[] args) { int[] array1 = newint [100]; for (int i = 0; i < array1.length; i++) { array1 [i] = i; } int[] array2 = newint [100]; System.arraycopy(array1, 0, array2, 0, array1.length); }} 3. 避免不需要的instanceof操作

如果左邊的對象的靜態(tài)類型等于右邊的,instanceof表達(dá)式返回永遠(yuǎn)為true。

[+]view code

publicclass 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"); }} 4. 避免不需要的造型操作

所有的類都是直接或者間接繼承自object。同樣,所有的子類也都隱含的“等于”其父類。那么,由子類造型至父類的操作就是不必要的了。如:

class dog extends unc

dog dog = new dog ();

unc animal = (unc)dog; // not necessary

object o = (object)dog; // not necessary

5. 如果只是查找單個(gè)字符的話,用charAt()代替startsWith [+]view code

publicclass PerformanceOptimization {

publicvoid method(String s) { if (s.startsWith("a")) { // violation // ... } } }

將'startsWith' 替換成'charAt()'

[+]view code

publicclass PerformanceOptimization {

publicvoid method(String s) { if ('a'==s.charAt(0) ) { // ... } } } 6. 使用移位操作來代替'a / b'和'a * b'操作

"/"和"*"都是一個(gè)很“昂貴”的操作,使用移位操作將會(huì)更快更有效。

[+]view code

publicclass PerformanceOptimization {

publicstaticfinalintnum = 16; publicvoid 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; // 不能轉(zhuǎn)換成位移操作 }

}

publicclass PerformanceOptimization { publicstaticfinalintnum = 16; publicvoid calculate(int a) { int mul = a * 4; // should be replaced with "a << 2". int mul2 = 8 * a; // should be replaced with "a << 3". inttemp = a * 3; // 不能轉(zhuǎn)換成位移操作 } }

注:除非是在一個(gè)非常大的循環(huán)內(nèi),性能非常重要,而且你很清楚你自己在做什么,方可使用這種方法。否則提高性能所帶來的程序可讀性的降低將是不合算的。

7. 不要在循環(huán)中調(diào)用synchronized(同步)方法

方法的同步需要消耗相當(dāng)大的資源,在一個(gè)循環(huán)中調(diào)用它絕對不是一個(gè)好主意。

[+]view code

publicclass PerformanceOptimization {

publicsynchronizedvoid method (Object o) { } privatevoid test () { for (int i = 0; i < vector.size(); i++) { method (vector.elementAt(i)); // violation } } private Vector vector = new Vector (5, 5); }

更正:不要在循環(huán)體中調(diào)用同步方法,如果必須同步的話,推薦以下方式: [+]view code

publicclass PerformanceOptimization {

publicvoid method (Object o) {} privatevoid test () { synchronized(this){//在一個(gè)同步塊中執(zhí)行非同步方法 for (int i = 0; i < vector.size(); i++) { method (vector.elementAt(i)); } } } private Vector vector = new Vector (5, 5); } 8. 將try/catch塊移出循環(huán)

把try/catch塊放入循環(huán)體內(nèi),會(huì)極大的影響性能,如果編譯jit被關(guān)閉或者你所使用的是一個(gè)不帶jit的jvm,性能會(huì)將下降21%之多!

[+]view code

publicclass PerformanceOptimization {

void method (FileInputStream fis) { for (int i = 0; i < 100; i++) { try { // violation _sum += fis.read(); } catch (Exception e) {} } } privateint_sum;} 9. 對于boolean值,避免不必要的等式判斷

將一個(gè)boolean值與一個(gè)true比較是一個(gè)恒等操作(直接返回該boolean變量的值). 移走對于boolean的不必要操作至少會(huì)帶來2個(gè)好處:

1)代碼執(zhí)行的更快 (生成的字節(jié)碼少了5個(gè)字節(jié));2)代碼也會(huì)更加干凈。 [+]view code

publicclass PerformanceOptimization {

boolean method (String string) { // return string.endsWith ("a") == true; // violation return string.endsWith ("a"); } } 10. 對于常量字符串,用'string' 代替 'stringbuffer'

常量字符串并不需要?jiǎng)討B(tài)改變長度。把stringbuffer換成string,如果確定這個(gè)string不會(huì)再變的話,這將會(huì)減少運(yùn)行開銷提高性能。

11. 用'StringTokenizer' 代替 'indexof()' 和'substring()'

字符串的分析在很多應(yīng)用中都是常見的。使用substring()來分析字符串容易導(dǎo)致java.lang.StringIndexOutOfBoundsException。而使用StringTokenizer類來分析字符串則會(huì)容易一些,效率也會(huì)高一些。

12. 使用三元運(yùn)算表達(dá)式替代"if (cond) XXX; else XXX;" 結(jié)構(gòu)。 13. 盡量不要在循環(huán)體中實(shí)例化變量

在循環(huán)體中實(shí)例化臨時(shí)變量將會(huì)增加內(nèi)存消耗

14. 盡可能的使用棧變量

如果一個(gè)變量需要經(jīng)常訪問,那么你就需要考慮這個(gè)變量的作用域了。static? local?還是實(shí)例變量?訪問靜態(tài)變量和實(shí)例變量將會(huì)比訪問局部變量跑的路徑要長的多。

[+]view code

publicclass PerformanceOptimization {

void getsum (int[] values) { for (int i=0; i < values.length; i++) { _sum += values[i]; // violation. } } void getsum2 (int[] values) { for (int i=0; i < values.length; i++) { _staticsum += values[i]; // violation. } } privateint_sum; privatestaticint_staticsum; }

更正:如果可能,請使用局部變量作為你經(jīng)常訪問的變量。可以按下面的方法來修改getsum()方法: [+]view code

void getsum (int[] values) {

int sum = _sum; // temporary local variable. for (int i=0; i < values.length; i++) { sum += values[i]; } _sum = sum; } 15. 與一個(gè)接口 進(jìn)行instanceof操作

基于接口的設(shè)計(jì)通常是件好事,因?yàn)樗试S有不同的實(shí)現(xiàn),而又保持靈活。只要可能,對一個(gè)對象進(jìn)行instanceof操作,以判斷它是否某一接口要比是否某一個(gè)類要快。

16. 盡量減少對變量的重復(fù)計(jì)算

比如

for(int i=0;i<list.size();i++)

應(yīng)修改為

for(int i=0,len=list.size();i<len;i++) 17. 考慮使用靜態(tài)方法

如果你沒有必要去訪問對象的外部,那么就使你的方法成為靜態(tài)方法。她會(huì)被更快地調(diào)用,因?yàn)樗恍枰粋€(gè)虛擬函數(shù)導(dǎo)向表。這同時(shí)也是一個(gè)很好的實(shí)踐,因?yàn)樗嬖V你如何區(qū)分方法的性質(zhì),調(diào)用這個(gè)方法不會(huì)改變對象的狀態(tài)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 炉霍县| 舞钢市| 交口县| 五原县| 新干县| 鹤峰县| 越西县| 托里县| 泰安市| 巴里| 肇源县| 福建省| 黑龙江省| 专栏| 丰县| 如东县| 宿迁市| 浮梁县| 永仁县| 辽宁省| 尚志市| 元江| 沧州市| 晋州市| 道真| 比如县| 嵊泗县| 安岳县| 驻马店市| 寿宁县| 柳江县| 宣城市| 马尔康县| 怀宁县| 漯河市| 阿合奇县| 宽甸| 疏附县| 保山市| 马尔康县| 莱阳市|