Android代碼效能最佳化技巧 (一)

來源:互聯網
上載者:User

標籤:android   blog   http   java   strong   for   ar   art   

轉載自 http://blog.csdn.net/leilu2008/article/details/6672979

 

我們大家都知道Android 2.2JIT效能有了本質的提高,不過對於老版本的程式提高Java執行效率還有很多語言特點來說,對於Java 1.5之後將會有明顯的改進。下面的例子來自SDK:    

[java] view plaincopy 
  1. static class Foo {  
  2.   
  3. int mSplat;  
  4.   
  5. }  
  6.   
  7. Foo[] mArray = ...  
  8.   
  9. //上面的靜態類Foo的執行效果和效能,我們分三個方法zero、one和two來做對比。  
  10.   
  11.     
  12. public void zero() { //大多數人可能簡單直接這樣寫  
  13.   
  14. int sum = 0;  
  15.   
  16. for (int i = 0; i < mArray.length; ++i) {  
  17.   
  18. sum += mArray[i].mSplat;  
  19.   
  20. }  
  21.   
  22. }  
  23.   
  24.     
  25. public void one() { //通過本機物件改進效能  
  26.   
  27. int sum = 0;  
  28.   
  29. Foo[] localArray = mArray;  
  30.   
  31. int len = localArray.length;  
  32.   
  33. for (int i = 0; i < len; ++i) {  
  34.   
  35. sum += localArray[i].mSplat;  
  36.   
  37. }  
  38.   
  39. }  
  40.   
  41.     
  42. public void two() { //推薦的方法,通過Java 1.5的新文法特性可以大幅改進效能  
  43.   
  44. int sum = 0;  
  45.   
  46. for (Foo a : mArray) {  
  47.   
  48. sum += a.mSplat;  
  49.   
  50. }  
  51.   
  52. }  



   zero()最慢one() 較快two() 最快,希望這些對大家有一些的協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.