if (s.startsWith(\ // ... } } }
更正
将'startsWith()' 替换成'charAt()'. public class PCTS {
private void method(String s) { if ('a' == s.charAt(0)) { // ... } } }
参考资料:
Dov Bulka, \Techniques\ 九、使用移位操作来代替'a / b'操作
\是一个很“昂贵”的操作,使用移位操作将会更快更有效。
例子:
public class SDIV {
public static final int NUM = 16; public void calculate(int a) {
int div = a / 4; // should be replaced with \ int div2 = a / 8; // should be replaced with \ int temp = a / 3; } }
更正:
public class SDIV {
public static final int NUM = 16; public void calculate(int a) { int div = a >> 2; int div2 = a >> 3;
int temp = a / 3; // 不能转换成位移操作 } }
十、使用移位操作代替'a * b'
同上。
[i]但我个人认为,除非是在一个非常大的循环内,性能非常重要,而且你很清楚你自己在做什么,方可使用这种方法。否则提高性能所带来的程序晚读性的降低将是不合算的。
例子:
public class SMUL {
public void calculate(int a) {
int mul = a * 4; // should be replaced with \ int mul2 = 8 * a; // should be replaced with \ int temp = a * 3; } }
更正:
package OPT;
public class SMUL {
public void calculate(int a) { int mul = a << 2; int mul2 = a << 3;
int temp = a * 3; // 不能转换 } }
十一、在字符串相加的时候,使用 ' ' 代替 \,如果该字符串只有一个字符的话
例子:
public class STR {
public void method(String s) {
String string = s + \ string = \ } }
更正:
将一个字符的字符串替换成' ' public class STR {
public void method(String s) { String string = s + 'd' string = \
} }
十二、不要在循环中调用synchronized(同步)方法
方法的同步需要消耗相当大的资料,在一个循环中调用它绝对不是一个好主意。
例子:
import java.util.Vector; public class SYN {
public synchronized void method (Object o) { }
private void test () {
for (int i = 0; i < vector.size(); i++) {
method (vector.elementAt(i)); // violation } }
private Vector vector = new Vector (5, 5); }
更正:
不要在循环体中调用同步方法,如果必须同步的话,推荐以下方式: import java.util.Vector; public class SYN {
public void method (Object o) {
}
private void test () {
synchronized{//在一个同步块中执行非同步方法 for (int i = 0; i < vector.size(); i++) { method (vector.elementAt(i)); } } }
private Vector vector = new Vector (5, 5); }
十三、将try/catch块移出循环
把try/catch块放入循环体内,会极大的影响性能,如果编译JIT被关闭或者你所使用的是一个不带JIT的JVM,性能会将下降21%之多!
例子:
import java.io.FileInputStream; public class TRY {
void method (FileInputStream fis) { for (int i = 0; i < size; i++) {
try { // violation _sum += fis.read(); } catch (Exception e) {} } }
private int _sum; }
更正:
将try/catch块移出循环 void method (FileInputStream fis) { try {
for (int i = 0; i < size; i++) { _sum += fis.read(); }
} catch (Exception e) {} }
参考资料:
Peter Haggar: \Addison Wesley, 2000, pp.81 – 83
十四、对于boolean值,避免不必要的等式判断
将一个boolean值与一个true比较是一个恒等操作(直接返回该boolean变量的值). 移走对于boolean的不必要操作至少会带来2个好处: 1)代码执行的更快 (生成的字节码少了5个字节); 2)代码也会更加干净。
例子:
public class UEQ {
boolean method (String string) {
return string.endsWith (\ } }
更正:
class UEQ_fixed
{
boolean method (String string) { return string.endsWith (\ } }
十五、对于常量字符串,用'String' 代替 'StringBuffer'
常量字符串并不需要动态改变长度。 例子:
public class USC { String method () {
StringBuffer s = new StringBuffer (\ String t = s + \ return t; } }
更正:
把StringBuffer换成String,如果确定这个String不会再变的话,这将会减少运行开销提高性能。
十六、用'StringTokenizer' 代替 'indexOf()' 和'substring()'
字符串的分析在很多应用中都是常见的。使用indexOf()和substring()来分析字符串容易导致StringIndexOutOfBoundsException。而使用StringTokenizer类来分析字符串则会容易一些,效率也会高一些。
例子:
public class UST {
void parseString(String string) { int index = 0;
while ((index = string.indexOf(\
System.out.println (string.substring(index, string.length())); } } }
参考资料:
Graig Larman, Rhett Guthrie: \Prentice Hall PTR, ISBN: 0-13-014260-3 pp. 282 – 283