我已經在Objective-c中實現了幾乎相同的代碼,它的運行速度比Java中快兩到三倍。我試圖找出哪些指令可能是資源密集度最高的,並且看看是否有更好的方法來做同樣的事情,這在Java中更加高效。Java - 什麼使這段代碼運行得更快?
這是從數據庫中讀取大型結果集的例程的一部分,並且對於返回的每個單詞,它會檢查該單詞是否可以由玩傢俱有的字母瓦片製成。它包括對空白瓷磚的支持,可以用作任何字母。一個空白的瓷磚將由一個下劃線字符表示。
基本上,對於從數據庫返回的每個單詞,我遍歷單詞的每個字母,並查看可用字母的播放器數組。如果我找到那封信,我將它從玩家陣列中移除並繼續前進。如果我沒有找到該字母,則丟棄該字詞並讀取下一個字詞。除非在播放器的數組中找到下劃線字符,否則我將使用該字母作爲字母,並將其從數組中移除。如果我到達數據庫單詞的字母數組的末尾並且已經「找到」每個單詞,那麼該單詞將保存在列表中。
我已經計時了整個函數的各個部分,並且數據庫查詢發生得非常快。只是這個遊標的處理非常緩慢。任何建議,將不勝感激!
if (c.moveToFirst()) {
do {
boolean found = false;
int aValue = 0;
int letterValue = 0;
// Word and Word's length from the database
String sWord = c.getString(0);
int wordLength = c.getInt(1);
// Refresh the Tile array, underscores sorted to the front
// sortedTiles sorted the players tiles {_,_,a,b,c}
char[] aTiles = sortedTiles.clone();
// Calculate the value of the word
for (int i = 0; i < wordLength; i++) {
// For each character in the database word
switch (sWord.charAt(i)) {
case 97:
letterValue = 1;
break;
case 98:
letterValue = 4;
break;
case 99:
letterValue = 4;
break;
case 100:
letterValue = 2;
break;
case 101:
letterValue = 1;
break;
case 102:
letterValue = 4;
break;
case 103:
letterValue = 3;
break;
case 104:
letterValue = 3;
break;
case 105:
letterValue = 1;
break;
case 106:
letterValue = 10;
break;
case 107:
letterValue = 5;
break;
case 108:
letterValue = 2;
break;
case 109:
letterValue = 4;
break;
case 110:
letterValue = 2;
break;
case 111:
letterValue = 1;
break;
case 112:
letterValue = 4;
break;
case 113:
letterValue = 10;
break;
case 114:
letterValue = 1;
break;
case 115:
letterValue = 1;
break;
case 116:
letterValue = 1;
break;
case 117:
letterValue = 2;
break;
case 118:
letterValue = 5;
break;
case 119:
letterValue = 4;
break;
case 120:
letterValue = 8;
break;
case 121:
letterValue = 3;
break;
case 122:
letterValue = 10;
break;
default:
letterValue = 0;
break;
} // switch
found = false;
// Underscores will be sorted to the front of the array,
// so start from the back so that we give
// real letters the first chance to be removed.
for (int j = aTiles.length - 1; j > -1; j--) {
if (aTiles[j] == sWord.charAt(i)) {
found = true;
// Increment the value of the word
aValue += letterValue;
// Blank out the player's tile so it is not reused
aTiles[j] = " ".charAt(0);
// I was removing the element from the array
// but I thought that might add overhead, so
// I switched to just blanking that letter out
// so that it wont be used again
//aTiles = removeItem(aTiles, j);
break;
}
if (aTiles[j] == cUnderscore) {
found = true;
// Blank out the player's tile so it is not reused
aTiles[j] = " ".charAt(0);
// I was removing the element from the array
// but I thought that might add overhead, so
// I switched to just blanking that letter out
// so that it wont be used again
//aTiles = removeItem(aTiles, j);
break;
}
} // for j
// If a letter in the word could not be fill by a tile
// or underscore, the word doesn't qualify
if (found == false) {
break;
}
} // for i
// If all the words letters were found, save the value and add to the list.
if (found == true) {
// if all the tiles were used it's worth extra points
String temp = aTiles.toString().trim();
if (temp.length() < 1) {
aValue += 35;
}
Word word = new Word();
word.word = sWord;
word.length = wordLength;
word.value = aValue;
listOfWords.add(word);
}
} while (c.moveToNext());
}
'「」.charAt(0)'可以簡單寫成'''' –
您是否正在iphone上運行objective-c版本以及android上的java版本?如果是這樣,是相似速度的硬件? – blizpasta
在通過每個字符開始循環之前,我會將字符串轉換爲字符數組。數組訪問可能比charAt()更快,即使charAt執行相同的操作,也會從堆棧中刪除額外的方法調用。還有+1 @Banthar爲這個小小的珍聞,對於來自C代碼的東西,這段代碼令人驚訝地反對。 – gnomed