如果这段代码优化?

时间:2013-02-16 04:14:53

标签: java performance

我需要改进这段代码,非常沉重,我没有找到另一种方法来创建

所有条件都需要验证,如果条件为真则不重要,必须继续寻找其他条件。

对我的英文GoogleTranslate使用抱歉

if (skill.getId() == 233 && getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 16 || getActiveClassId() == 30 || getActiveClassId() == 17 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 105 || getActiveClassId() == 98 || getActiveClassId() == 112 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 116 || getActiveClassId() == 116 || getActiveClassId() == 28 || getActiveClassId() == 41 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 111 || getActiveClassId() == 96) {
    removeSkillById(233); // Light Armor
} else {
    // Nothing...
}
if (skill.getId() == 227 && getActiveClassId() == 36 || getActiveClassId() == 9 || getActiveClassId() == 37 || getActiveClassId() == 23 || getActiveClassId() == 24 || getActiveClassId() == 8 || getActiveClassId() == 48 || getActiveClassId() == 108 || getActiveClassId() == 92 || getActiveClassId() == 109 || getActiveClassId() == 101 || getActiveClassId() == 102 || getActiveClassId() == 93 || getActiveClassId() == 114 || getActiveClassId() == 16 || getActiveClassId() == 30 || getActiveClassId() == 17 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 105 || getActiveClassId() == 98 || getActiveClassId() == 112 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116 || getActiveClassId() == 28 || getActiveClassId() == 41 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 111 || getActiveClassId() == 96) {
    removeSkillById(227); // Light Armor
} else {
    // Nothing...
}
if (skill.getId() == 236 && getActiveClassId() == 36 || getActiveClassId() == 9 || getActiveClassId() == 37 || getActiveClassId() == 23 || getActiveClassId() == 24 || getActiveClassId() == 8 || getActiveClassId() == 48 || getActiveClassId() == 108 || getActiveClassId() == 92 || getActiveClassId() == 109 || getActiveClassId() == 101 || getActiveClassId() == 102 || getActiveClassId() == 93 || getActiveClassId() == 114 || getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116 || getActiveClassId() == 28 || getActiveClassId() == 41 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 111 || getActiveClassId() == 96) {
    removeSkillById(236); // Light Armor
} else {
    // Nothing...
}
if (skill.getId() == 252 && getActiveClassId() == 36 || getActiveClassId() == 9 || getActiveClassId() == 37 || getActiveClassId() == 23 || getActiveClassId() == 24 || getActiveClassId() == 8 || getActiveClassId() == 48 || getActiveClassId() == 108 || getActiveClassId() == 92 || getActiveClassId() == 109 || getActiveClassId() == 101 || getActiveClassId() == 102 || getActiveClassId() == 93 || getActiveClassId() == 114 || getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 16 || getActiveClassId() == 30 || getActiveClassId() == 17 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 105 || getActiveClassId() == 98 || getActiveClassId() == 112 || getActiveClassId() == 28 || getActiveClassId() == 41 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 111 || getActiveClassId() == 96) {
    removeSkillById(252); // Light Armor
} else {
    // Nothing...
}
if (skill.getId() == 258 && getActiveClassId() == 36 || getActiveClassId() == 9 || getActiveClassId() == 37 || getActiveClassId() == 23 || getActiveClassId() == 24 || getActiveClassId() == 8 || getActiveClassId() == 48 || getActiveClassId() == 108 || getActiveClassId() == 92 || getActiveClassId() == 109 || getActiveClassId() == 101 || getActiveClassId() == 102 || getActiveClassId() == 93 || getActiveClassId() == 114 || getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 16 || getActiveClassId() == 30 || getActiveClassId() == 17 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 105 || getActiveClassId() == 98 || getActiveClassId() == 112 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116) {
    removeSkillById(258); // Light Armor
} else {
    // Nothing...
}
if (skill.getId() == 231 && getActiveClassId() == 6 || getActiveClassId() == 5 || getActiveClassId() == 33 || getActiveClassId() == 20 || getActiveClassId() == 91 || getActiveClassId() == 90 || getActiveClassId() == 106 || getActiveClassId() == 99 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116 || getActiveClassId() == 17 || getActiveClassId() == 98) {
    removeSkillById(231); // Heavy Armor
} else {
    // Nothing...
}
if (skill.getId() == 232 && getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116 || getActiveClassId() == 17 || getActiveClassId() == 98) {
    removeSkillById(232); // Heavy Armor
} else {
    // Nothing...
}
if (skill.getId() == 253 && getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 6 || getActiveClassId() == 5 || getActiveClassId() == 33 || getActiveClassId() == 20 || getActiveClassId() == 91 || getActiveClassId() == 90 || getActiveClassId() == 106 || getActiveClassId() == 99 || getActiveClassId() == 17 || getActiveClassId() == 98) {
    removeSkillById(253); // Heavy Armor
} else {
    // Nothing...
}
if (skill.getId() == 259 && getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 6 || getActiveClassId() == 5 || getActiveClassId() == 33 || getActiveClassId() == 20 || getActiveClassId() == 91 || getActiveClassId() == 90 || getActiveClassId() == 106 || getActiveClassId() == 99 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116) {
    removeSkillById(259); // Heavy Armor
} else {
    // Nothing...
}
if (skill.getId() == 234 && getActiveClassId() == 16 || getActiveClassId() == 17 || getActiveClassId() == 30 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 98 || getActiveClassId() == 105 || getActiveClassId() == 112 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116) {
    removeSkillById(234); // Robe Mastery
} else {
    // Nothing...
}
if (skill.getId() == 235 && getActiveClassId() == 28 || getActiveClassId() == 13 || getActiveClassId() == 41 || getActiveClassId() == 12 || getActiveClassId() == 40 || getActiveClassId() == 27 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 95 || getActiveClassId() == 111 || getActiveClassId() == 94 || getActiveClassId() == 110 || getActiveClassId() == 103 || getActiveClassId() == 96 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116) {
    removeSkillById(235); // Robe Mastery
} else {
    // Nothing...
}
if (skill.getId() == 251 && getActiveClassId() == 28 || getActiveClassId() == 13 || getActiveClassId() == 41 || getActiveClassId() == 12 || getActiveClassId() == 40 || getActiveClassId() == 27 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 95 || getActiveClassId() == 111 || getActiveClassId() == 94 || getActiveClassId() == 110 || getActiveClassId() == 103 || getActiveClassId() == 96 || getActiveClassId() == 16 || getActiveClassId() == 17 || getActiveClassId() == 30 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 98 || getActiveClassId() == 105 || getActiveClassId() == 112) {
    removeSkillById(251); // Robe Mastery
} else {
    // Nothing...
}

任何帮助我?谢谢!

2 个答案:

答案 0 :(得分:2)

你可以做一些事情来优化它。

  1. 删除以下代码块,它们是不必要的:

    else 
    {
        // Nothing..
    }
    
  2. 接下来,将函数的返回值保存到局部变量,以节省多次调用同一函数的开销。

    int activeClassId = getActiveClassId(), skillId = skill.getId();
    
  3. 使用List存储可能的值,然后使用List.contains()。例如,您现在可以对此进行转换:

    if(skill.getId() == 252 && getActiveClassId() == 36 || getActiveClassId() == 9 || getActiveClassId() == 37 || getActiveClassId() == 23 || getActiveClassId() == 24 || getActiveClassId() == 8 || getActiveClassId() == 48 || getActiveClassId() == 108 || getActiveClassId() == 92 || getActiveClassId() == 109 || getActiveClassId() == 101 || getActiveClassId() == 102 || getActiveClassId() == 93 || getActiveClassId() == 114 || getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 16 || getActiveClassId() == 30 || getActiveClassId() == 17 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 105 || getActiveClassId() == 98 || getActiveClassId() == 112 || getActiveClassId() == 28 || getActiveClassId() == 41 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 111 || getActiveClassId() == 96)
    

    进入这个:

    List<Integer> validClassIds = Arrays.asList( 36, 9, 37, 23, 24, 8, 48, 108, 92, 109, 101, 102, 93, 114, 55, 46, 2, 3, 57, 117, 113, 88, 89, 118, 16, 30, 17, 43, 97, 105, 98, 112, 28, 41, 14, 104, 111, 96);
    
    if( skillId == 252 && validClassIds.contains( activeClassId))
    
  4. 如果您不想使用O(n)进行List.contains()搜索,则可以使用HashSet而不是List。更多信息here

答案 1 :(得分:1)

我从算法角度提出建议。

使用 Huffman coding 通过调整if-else条件序列来最小化比较时间。如果您有数据可能性的统计信息。