編輯:初級開發
static class Foo {
int mSplat;
}
Foo[] mArray = ...
上面的靜態類Foo的執行效果和性能,我們分三個方法zero、one和two來做對比。
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() { //推薦的方法,通過Java 1.5的新語法特性可以大幅改進性能
int sum = 0;
for (Foo a : mArray) {
sum += a.mSplat;
}
}
zero() is slowest, because the JIT can't yet optimize away the cost of getting the array length once for every iteration through the loop.
one() is faster. It pulls everything out into local variables, avoiding the lookups. Only the array length offers a performance benefit.
two() is fastest for devices without a JIT, and indistinguishable from one() for devices with a JIT. It uses the enhanced for loop syntax introduced in version 1.5 of the Java programming language.
l 使用Intent物件雖然透過Intent並非最快速,但卻是最有彈性的。無論是同一進程或是跨進程的溝通都可以使用它。例如:/* ===== EX-02 ======
在android開發中ListView是比較常用的組件,它以列表的形式展示具體內容,並且能夠根據數據的長度自適應顯示。抽空把對ListVIEw的使用做了整理,並寫了個小
下面展示一段在android1.5上讀取手機通訊錄的代碼1 //鏈接通訊錄數據庫 2 ContentResolver content = getContentResol
前言關鍵字: DigitalClock date formatandroid的DigitalClock並沒有設置輸出格式的屬性或方法,但是可以通過繼承重寫來實現,見正文