android效能最佳化基礎篇(迴圈的最佳化)

來源:互聯網
上載者:User

android效能最佳化基礎篇(迴圈的最佳化)

從Android開發人員網址上學習到的效能最佳化知識。
1.迴圈遍曆的最佳化
需要對某集合進行迴圈遍曆時,若該資料集實現了Iterable介面,可用增強型的for來進行迴圈(for-each),而不用手動寫length個長度進行一個一個訪問。
static class Foo {    int mSplat;}Foo[] mArray = ...public void zero() {    int sum = 0;    for (int i = 0; i < mArray.length; ++i) {        sum += mArray[i].mSplat;    }}public void one() {    int sum = 0;    Foo[] localArray = mArray;    int len = localArray.length;    for (int i = 0; i < len; ++i) {        sum += localArray[i].mSplat;    }}public void two() {    int sum = 0;    for (Foo a : mArray) {        sum += a.mSplat;    }}
以上例子中,zero的效能最差,one較zero更優,僅僅只是將localArray的length緩衝起來了,避免每次都去取一下length;而two的效能最好,即遍曆的速度最快。在沒有JIT的VM上運行最快,而在有JIT的VM上其與one的效能相當。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.