在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快。例子:
import java.util.vector;
public class cel {
public void method (vector vector) {
for (int i = 0; i < vector.size (); i++) {
...
}
}
}更正:
import java.util.vector;
public class cel {
public void method (vector vector) {
// 避免每次都去过去size()值
for (int i = 0, size = vector.size(); i < size; i++) {
...
}
}
}Java在给集合扩充大小时,如:ArrayList集合类,是通过System.arraycopy方法将原有数组复制到新数组来实现。因此每次扩充容量就要执行一次数组拷贝,很费事。通常,默认的10个元素大小是不够的。你最好能准确的估计你所需要的最佳大小。 例子:
import java.util.ArrayList;
public class dic {
public ArrayList v = new ArrayList();
public void addobjects (object[] o) {
for (int i = 0; i< o.length;i++) {
v.add(o);
}
}
}更正: 自己设定初始大小。
import java.util.ArrayList;
public class dic {
public ArrayList v = new ArrayList(30);
public void addobjects (object[] o) {
for (int i = 0; i< o.length;i++) {
v.add(o);
}
}
}参考资料:Java源代码
程序中使用到的资源应当被释放,以避免资源泄漏。这最好在finally块中去做。不管程序执行的结果如何,finally块总是会执行的,以确保资源的正确关闭。 例子:
import java.io.*;
public class cs {
public static void main (string args[]) {
cs cs = new cs ();
cs.method ();
}
public void method () {
try {
FileInputStream fis = new FileInputStream ("cs.java");
int count = 0;
while ( fis.read () != -1 ) {
count++;
}
system.out.println (count);
fis.close ();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}更正: 在最后一个catch后添加一个finally块
import java.io.*;
public class cs {
public static void main (string args[]) {
cs cs = new cs ();
cs.method ();
}
public void method () {
FileInputStream fis = null;
try {
fis = new FileInputStream ("cs.java");
int count = 0;
while ( fis.read () != -1 ) {
count++;
}
system.out.println (count);
fis.close ();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
} finally {
if ( null != fis ) {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
fis = null;
}
}
}
}
}因为System.arraycopy()是java系统自带的,因此性能应该还是可以。要比通过循环来复制数组快的多。例子:
public class irb {
void method () {
int[] array1 = new int [100];
for (int i = 0; i < array1.length; i++) {
array1 [i] = i;
}
int[] array2 = new int [100];
for (int i = 0; i < array2.length; i++) {
array2 [i] = array1 [i];
}
}
}更正:
public class irb {
void method () {
int[] array1 = new int [100];
for (int i = 0; i < array1.length; i++) {
array1 [i] = i;
}
int[] array2 = new int [100];
system.arraycopy(array1, 0, array2, 0, 100);
}
}简单的getter/setter方法应该被置成final,这会告诉编译器,这个方法不会被重载,所以,可以变成”inlined” 例子:
public class maf {
private int _size;
public void setsize (int size) {
_size = size;
}
}更正:
public class daf_fixed {
private int _size;
final public void setsize (int size) {
_size = size;
}
}如果左边的对象的静态类型等于右边的,instanceof表达式返回永远为true。例子:
public class uiso {
public uiso () {}
}
class dog extends uiso {
void method (dog dog, uiso u) {
dog d = dog;
if (d instanceof uiso) // always true.
system.out.println("dog is a uiso");
uiso uiso = u;
if (uiso instanceof object) // always true.
system.out.println("uiso is an object");
}
}更正:删掉不需要的instanceof操作。
class dog extends uiso {
void method () {
dog d;
system.out.println ("dog is an uiso");
system.out.println ("uiso is an uiso");
}
}所有的类都是直接或者间接继承自object。同样,所有的子类也都隐含的“等于”其父类。那么,由子类造型至父类的操作就是不必要的了。例子:
class unc {
string _id = "unc";
}
class dog extends unc {
void method () {
dog dog = new dog ();
unc animal = (unc)dog; // not necessary.
object o = (object)dog; // not necessary.
}
}更正:
class dog extends unc {
void method () {
dog dog = new dog();
unc animal = dog;
object o = dog;
}
}用一个字符作为参数调用startswith()也会工作的很好,但从性能角度上来看,调用用string api无疑是错误的! 例子:
public class pcts {
private void method(string s) {
if (s.startswith("a")) { // violation
// ...
}
}
}更正:将'startswith()' 替换成'charat()'.
public class pcts {
private void method(string s) {
if ('a' == s.charat(0)) {
// ...
}
}
}"/"是一个很“昂贵”的操作,使用移位操作将会更快更有效。 例子:
public class sdiv {
public static final int num = 16;
public void calculate(int a) {
int div = a / 4; // should be replaced with "a >> 2".
int div2 = a / 8; // should be replaced with "a >> 3".
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; // 不能转换成位移操作
}
}我个人认为,除非是在一个非常大的循环内,性能非常重要,而且你很清楚你自己在做什么,方可使用这种方法。否则提高性能所带来的程序晚读性的降低将是不合算的。 例子:
public class smul {
public void calculate(int a) {
int mul = a * 4; // should be replaced with "a << 2".
int mul2 = 8 * a; // should be replaced with "a << 3".
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 + "d" // violation.
string = "abc" + "d" // violation.
}
}更正: 将一个字符的字符串替换成' '
public class str {
public void method(string s) {
string str = s + 'd'
str = "abc" + 'd'
}
}方法的同步需要消耗相当大的资料,在一个循环中调用它绝对不是一个好主意。 例子:
import java.util.vector;
public class syn {
private vector vector = new vector (5, 5);
public synchronized void method (object o) {
...
}
private void test () {
for (int i = 0; i < vector.size(); i++) {
method (vector.elementat(i)); // violation
}
}
}更正:不要在循环体中调用同步方法,如果必须同步的话,推荐以下方式:
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 {
private int _sum;
public void method (FileInputStream fis) {
for (int i = 0; i < size; i++) {
try {
_sum += fis.read();
} catch (Exception e) {
...
}
}
}
}更正:将try/catch块移出循环
public void method (FileInputStream fis) {
try {
for (int i = 0; i < size; i++) {
_sum += fis.read();
}
} catch (Exception e) {}
}十四、对于boolean值,避免不必要的等式判断
将一个boolean值与一个true比较是一个恒等操作(直接返回该boolean变量的值). 移走对于boolean的不必要操作至少会带来2个好处:
1)代码执行的更快 (生成的字节码少了5个字节);
2)代码也会更加干净 。
例子:
public class ueq {
boolean method (string str) {
return str.endswith ("a") == true; // violation
}
}更正:
public class ueq_fixed {
boolean method (String str) {
return str.endswith ("a");
}
}十五、对于常量字符串,用'String' 代替 'StringBuffer'
常量字符串并不需要动态改变长度。例子:
public class usc {
public String method () {
StringBuffer s = new StringBuffer ("hello");
String t = s + "world!";
return t;
}
}更正:把StringBuffer换成String,如果确定这个String不会再变的话,这将会减少运行开销提高性能。
十六、用'StringTokenizer' 代替 'indexOf()' 和'substring()'
字符串的分析在很多应用中都是常见的。使用indexOf()和substring()来分析字符串容易导致StringIndexOutOfBoundsException。而使用StringTokenizer类来分析字符串则会容易一些,效率也会高一些。例子:
public class ust {
void parsestring(string str) {
int index = 0;
while ((index = str.indexOf(".", index)) != -1) {
System.out.println (str.substring(index, str.length()));
}
}
}十七、使用条件操作符替代"if (cond) return; else return;" 结构
条件操作符更加的简捷 例子:
public class if {
public int method(boolean isdone) {
if (isdone) {
return 0;
} else {
return 10;
}
}
}更正:
public class if {
public int method(boolean isdone) {
return (isdone ? 0 : 10);
}
}十八、使用条件操作符代替"if (cond) a = b; else a = c;" 结构
例子:
public class ifas {
private int _value = 0;
public void method(boolean istrue) {
if (istrue) {
_value = 0;
} else {
_value = 1;
}
}
}更正:
public class ifas {
private int _value = 0;
public void method(boolean istrue) {
_value = (istrue ? 0 : 1); // compact expression.
}
}十九、不要在循环体中实例化变量
在循环体中实例化临时变量将会增加内存消耗 例子:
import java.util.vector;
public class loop {
public void method (Vector v) {
for (int i=0;i < v.size();i++) {
Object o = new Object();
o = v.elementat(i);
}
}
}更正:在循环体外定义变量,并反复使用
import java.util.vector;
public class loop {
public void method (Vector v) {
Object o;
for (int i=0;i<v.size();i++) {
o = v.elementat(i);
}
}
}二十、确定 StringBuffer的容量
Stringbuffer的构造器会创建一个默认大小(通常是16)的字符数组。在使用中,如果超出这个大小,就会重新分配内存,创建一个更大的数组,并将原先的数组复制过来,再丢弃旧的数组。在大多数情况下,你可以在创建StringBuffer的时候指定大小,这样就避免了在容量不够的时候自动增长,以提高性能。 例子:
public class rsbc {
public void method () {
StringBuffer buffer = new StringBuffer(); // violation
buffer.append ("hello");
}
}更正:为StringBuffer提供寝大小。
public class rsbc {
private final int max = 100;
public void method () {
StringBuffer buffer = new StringBuffer(max);
buffer.append ("hello");
}
}二十一、尽可能的使用栈变量
如果一个变量需要经常访问,那么你就需要考虑这个变量的作用域了。static? local? 还是实例变量?访问静态变量和实例变量将会比访问局部变量多耗费2-3个时钟周期。 例子:
public class usv {
private int _sum;
private static int _staticsum;
public void getsum (int[] values) {
for (int i=0; i < value.length; i++) {
_sum += value[i]; // violation.
}
}
public void getsum2 (int[] values) {
for (int i=0; i < value.length; i++) {
_staticsum += value[i];
}
}
}更正:如果可能,请使用局部变量作为你经常访问的变量。 你可以按下面的方法来修改getsum()方法:
public void getsum (int[] values) {
int sum = _sum; // temporary local variable.
for (int i=0; i < value.length; i++) {
sum += value[i];
}
_sum = sum;
}二十二、不要总是使用取反操作符(!)
取反操作符(!)降低程序的可读性,所以不要总是使用。例子:
public class dun {
boolean method (boolean a, boolean b) {
if (!a) {
return !a;
} else {
return !b;
}
}
}更正: 如果可能不要使用取反操作符(!)
二十三、与一个接口进行instanceof操作
基于接口的设计通常是件好事,因为它允许有不同的实现,而又保持灵活。只要可能,对一个对象进行instanceof操作,以判断它是否某一接口要比是否某一个类要快。 例子:
public class insof {
private void method (object o) {
if (o instanceof interfacebase) { ... } // better
if (o instanceof classbase) { ... } // worse.
}
}
class classbase {}
interface interfacebase {}