gpt4 book ai didi

Java - 什么可以使这段代码运行得更快?

转载 作者:太空狗 更新时间:2023-10-29 15:43:35 25 4
gpt4 key购买 nike

我在 Objective-c 中实现了几乎相同的代码,它的运行速度比在 Java 中快两到三倍。我试图弄清楚哪些指令可能是资源最密集的,看看是否有更好的方法来做同样的事情,在 Java 中效率更高。

这是从数据库中读取大型结果集的例程的一部分,对于返回的每个单词,它会检查该单词是否可以由玩家拥有的字母拼贴组成。它包括对空白图 block 的支持,可以用作任何字母。空白磁贴将由下划线字符表示。

基本上,对于从数据库返回的每个单词,我遍历单词的每个字母,并查看可用字母的玩家数组。如果我找到那封信,我将其从玩家数组中删除并继续前进。如果我没有找到这封信,这个词就会被丢弃,然后读取下一个词。除非,我在玩家的数组中找到一个下划线字符,然后,我会用它来表示字母,并将其从数组中删除。如果我到达数据库单词字母数组的末尾并“找到”每个字母,那么该单词将保存在列表中。

我已经对整个函数的各个部分进行了计时,数据库查询速度非常快。只是这个游标的处理速度很慢。如有任何建议,我们将不胜感激!

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());
}

最佳答案

我不确切地知道您的代码将大部分时间花在哪里。您应该对此进行分析。但我会用表查找替换您的长 switch 语句:

// In the class:
private static final int[] letterValues = {
1, 4, 4, 2, 1, // etc.
}

// In the code:

// Calculate the value of the word
for (int i = 0; i < wordLength; i++) {

// For each character in the database word
char ch = sWord.charAt(i);
if (ch >= 97 && ch <= 122) {
letterValue = letterValues[ch - 97];
} else {
letterValue = 0;
}

// the rest of your code

这可能比 switch 语句快得多。

编辑:我注意到在您的 j 循环中,您正在为每个 j 值调用 sWord.charAt(i)。您可以通过将该函数调用排除在循环之外来加快速度。如果您使用我的代码,您只需使用 ch 代替 sWord.charAt(i)

附言从风格上讲,最好编写 if (found) { ... 而不是 if (found == true) { ...。同样使用 if (!found) { 而不是 if (found == false) {

关于Java - 什么可以使这段代码运行得更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7781412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com