一:良好的编程习惯
1.避免在循环结构中使用复制表达式
int count = list.size();
for(int i=0;i<count;i++)
2.在finally块中关闭stream
程序中使用到的资源,应当被释放,以免资源泄露,这最好在finally 中操作。不管程序执行结果如何,确保资源正确关闭。
3.使用System.arraycopy() 代替循环复制数组。
例子:
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]; // violation
}
}
}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);
}
}4.让访问实例内变量的getter/setter 方法变成final
简单的gettter/setter 方法应该被设置成final ,这会告诉编译器,这个方法不会被重载。所以可以变成final例子:
class maf {
public void setsize (int size) {
_size = size;
}
private int _size;
}class daf_fixed {
final public void setsize (int size) {
_size = size;
}
private int _size;
}5.避免不必要的instanceof 操作
如果左边的对象的静态类型等译右边的,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");
}
}class dog extends uiso {
void method () {
dog d;
system.out.println ("dog is an uiso");
system.out.println ("uiso is an uiso");
}
}
6.避免不必要的造型操作
所有的类都是直接或间接的继承自Object ,同样所有的子类都隐含的等于其父类。那么子类造型至父类的操作就是不必要的了。
八、如果只是查找单个字符的话,用charat()代替startswith()
用一个字符作为参数调用startswith()也会工作的很好,但从性能角度上来看,调用用string api无疑是错误的!
例子:
public class pcts {
private void method(string s) {
if (s.startswith("a")) { // violation
// ...
}
}
}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 string = s + ‘d‘
string = "abc" + ‘d‘
}
}
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);
}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;
} void method (fileinputstream fis) {
try {
for (int i = 0; i < size; i++) {
_sum += fis.read();
}
} catch (exception e) {}
}public class ueq
{
boolean method (string string) {
return string.endswith ("a") == true; // violation
}
}class ueq_fixed
{
boolean method (string string) {
return string.endswith ("a");
}
}public class usc {
string method () {
stringbuffer s = new stringbuffer ("hello");
string t = s + "world!";
return t;
}
}public class ust {
void parsestring(string string) {
int index = 0;
while ((index = string.indexof(".", index)) != -1) {
system.out.println (string.substring(index, string.length()));
}
}
}
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);
}
}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);
}
}
}public class rsbc {
void method () {
stringbuffer buffer = new stringbuffer(); // violation
buffer.append ("hello");
}
}public class rsbc {
void method () {
stringbuffer buffer = new stringbuffer(max);
buffer.append ("hello");
}
private final int max = 100;
}public class usv {
void getsum (int[] values) {
for (int i=0; i < value.length; i++) {
_sum += value[i]; // violation.
}
}
void getsum2 (int[] values) {
for (int i=0; i < value.length; i++) {
_staticsum += value[i];
}
}
private int _sum;
private static int _staticsum;
} 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;
}
}public class insof {
private void method (object o) {
if (o instanceof interfacebase) { } // better
if (o instanceof classbase) { } // worse.
}
}
class classbase {}
interface interfacebase {}二.数据库优化
Hibernate
批量处理其实从性能上考虑,它是很不可取的,浪费了很大的内存。
从它的机制上讲,
Hibernate
它是先把符合条件的数据查出来,放到内存当中,
然后再进行操作。实际使用下来性能非常不理想。解决方案可分为以下四种:
1
:绕过
Hibernate API
,直接通过
JDBC API
来做,这个方法性能上是比
较好的。也是最快的
.
2
:运用存储过程。代价是移植性降低
3
:缓存。对于构建的业务系统,如果有些数据要经常要从数据库中读取,
同时,这些数据又不经常变化,这些数据就可以在系统中缓存起来,使
用时直接读取缓存,而不用频繁的访问数据库读取数据。缓存工作可以
在系统初始化时一次性读取数据,特别是一些只读的数据,当数据更新
时更新数据库内容,同时更新缓存的数据值。
4
:增加数据库连接池的大小。
三.
Java
虚拟机堆和垃圾回收设置
任何
Java
应用的性能调整基础都涉及到堆的大小和垃圾回收设置。
二.数据库优化
Hibernate
批量处理其实从性能上考虑,它是很不可取的,浪费了很大的内存。
从它的机制上讲,
Hibernate
它是先把符合条件的数据查出来,放到内存当中,
然后再进行操作。实际使用下来性能非常不理想。解决方案可分为以下四种:
1
:绕过
Hibernate API
,直接通过
JDBC API
来做,这个方法性能上是比
较好的。也是最快的
.
2
:运用存储过程。代价是移植性降低
3
:缓存。对于构建的业务系统,如果有些数据要经常要从数据库中读取,
同时,这些数据又不经常变化,这些数据就可以在系统中缓存起来,使
用时直接读取缓存,而不用频繁的访问数据库读取数据。缓存工作可以
在系统初始化时一次性读取数据,特别是一些只读的数据,当数据更新
时更新数据库内容,同时更新缓存的数据值。
4
:增加数据库连接池的大小。
三.
Java
虚拟机堆和垃圾回收设置
任何
Java
应用的性能调整基础都涉及到堆的大小和垃圾回收设置。
原文:http://blog.csdn.net/u011598529/article/details/20226637