JAVA程序性能优化(转载)

[复制链接]
查看11 | 回复6 | 2011-5-7 01:45:08 | 显示全部楼层 |阅读模式
一、避免在循环条件中使用复杂表达式
在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,

使循环条件值不变的话,程序将会运行的更快。
例子:
import java.util.Vector;
class CEL {
void method (Vector vector) {
for (int i = 0; i10, Vector needs to expand
for (int i = 0; i> 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; // 不能转换成位移操作
}
}
十、使用移位操作代替‘a * b‘
同上。
但我个人认为,除非是在一个非常大的循环内,性能非常重要,而且你很清楚你自己在

什么,方可使用这种方法。否则提高性能所带来的程序晚读性的降低将是不合算的。
例子:
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 string = s + ‘d‘
string = "abc" + ‘d‘
}
}
十二、不要在循环中调用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: "Practical Java - Programming Language Guide".
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 ("a&quot

== true; // Violation
}
}
更正:
class UEQ_fixed
{
boolean method (String string) {
return string.endsWith ("a&quot

;
}
}
十五、对于常量字符串,用‘String‘ 代替 ‘StringBuffer‘常量字符串并不需要动态
改变长度。
例子:
public class USC {
String method () {
StringBuffer s = new StringBuffer ("Hello&quot

;
String t = s + "World!";
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(".", index)) != -1) {

System.out.println (string.substring(index, string.length()));
}
}
}
参考资料:
Graig Larman, Rhett Guthrie: "Java 2 Performance and Idiom Guide"
Prentice Hall PTR, ISBN: 0-13-014260-3 pp. 282 – 283
十七、使用条件操作符替代"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 {
void method(boolean isTrue) {
if (isTrue) {

_value = 0;
} else {

_value = 1;
}
}
private int _value = 0;
}
更正:
public class IFAS {
void method(boolean isTrue) {
_value = (isTrue ? 0 : 1); // compact expression.
}
private int _value = 0;
}
十九、不要在循环体中实例化变量
在循环体中实例化临时变量将会增加内存消耗
例子:

import java.util.Vector;
public class LOOP {
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 {
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 {
void method () {
StringBuffer buffer = new StringBuffer(); // violation
buffer.append ("hello&quot

;
}
}


更正:

为StringBuffer提供寝大小。

public class RSBC {
void method () {
StringBuffer buffer = new StringBuffer(MAX);
buffer.append ("hello");
}
private final int MAX = 100;
}
参考资料:
Dov Bulka, "Java Performance and Scalability Volume 1: Server-Side Programming
Techniques" Addison Wesley, ISBN: 0-201-70429-3 p.30 – 31
二十一、尽可能的使用栈变量
如果一个变量需要经常访问,那么你就需要考虑这个变量的作用域了。static? local?还

实例变量?访问静态变量和实例变量将会比访问局部变量多耗费2-3个时钟周期。
例子:
public class USV {
void getSum (int[] values) {
for (int i=0; i < value.length; i++) {

_sum += value; // violation.
}
}
void getSum2 (int[] values) {
for (int i=0; i < value.length; i++) {

_staticSum += value;
}
}
private int _sum;
private static int _staticSum;
}
更正:

如果可能,请使用局部变量作为你经常访问的变量。
你可以按下面的方法来修改getSum()方法:

void getSum (int[] values) {
int sum = _sum;// temporary local variable.
for (int i=0; i < value.length; i++) {
sum += value;
}
_sum = sum;
}
参考资料:

Peter Haggar: "Practical Java - Programming Language Guide".
Addison Wesley, 2000, pp.122 – 125
二十二、不要总是使用取反操作符(!)
取反操作符(!)降低程序的可读性,所以不要总是使用。
例子:
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 {}
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
LZ辛苦了,谢谢分享!



回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
好文!!!谢谢楼主
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
学习
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
有些不一定要可以追求得。比如*和/。
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
好长,不过不错。
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
看用途了,有些情况下系统资源不够的时候对效率的追求更多
不过现在好像可读性较重要……
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行