提高java代码的效率

来源:互联网 发布:大乐透算法必中5红万能 编辑:程序博客网 时间:2024/04/30 12:21
原文地址:提高java代码的效率作者:coderMan

一、避免在循环条件中使用复杂表达式
在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快。例子:

  1. importjava.util.Vector;
  2. classCEL {
  3.     voidmethod (Vectorvector) {
  4.        for (int i = 0; i <</span> vector.size (); i++)   // Violation
  5.           ; //...
  6.     }
  7. }

更正:

  1. classCEL_fixed {
  2.     voidmethod (Vectorvector) {
  3.        int size = vector.size ()
  4.        for (int i = 0; i <</span> size; i++)
  5.           ; //...
  6.     }
  7. }

二、为'Vectors' 和 'Hashtables'定义初始大小
JVM为Vector扩充大小的时候需要重新创建一个更大的数组,将原原先数组中的内容复制过来,最后,原先的数组再被回收。可见Vector容量的扩大是一个颇费时间的事。
通常,默认的10个元素大小是不够的。你最好能准确的估计你所需要的最佳大小。例子:

  1. importjava.util.Vector;
  2. publicclassDIC {
  3.     publicvoidaddObjects (Object[]o) {
  4.        // if length> 10, Vector needs to expand
  5.        for (int i = 0; i<</span> o.length;i++) {   
  6.           v.add(o);   // capacitybefore it can add more elements.
  7.        }
  8.     }
  9.     publicVectorv = new Vector(); // no initialCapacity.
  10. }

更正:
自己设定初始大小。

  1. publicVectorv = new Vector(20); 
  2. publicHashtablehash = new Hashtable(10);

三、在finally块中关闭Stream
程序中使用到的资源应当被释放,以避免资源泄漏。这最好在finally块中去做。不管程序执行的结果如何,finally块总是会执行的,以确保资源的正确关闭。
四、使用'System.arraycopy ()'代替通过来循环复制数组,例子:

  1. publicclassIRB
  2. {
  3.     voidmethod () {
  4.        int[] array1 = new int [100];
  5.        for (int i = 0; i <</span> array1.length; i++) {
  6.           array1 [i] = i;
  7.        }
  8.        int[] array2 = new int [100];
  9.        for (int i = 0; i <</span> array2.length; i++) {
  10.           array2 [i] = array1 [i];               //Violation
  11.        }
  12.     }
  13. }


更正:

  1. publicclassIRB
  2. {
  3.     voidmethod () {
  4.        int[] array1 = new int [100];
  5.        for (int i = 0; i <</span> array1.length; i++) {
  6.           array1 [i] = i;
  7.        }
  8.        int[] array2 = new int [100];
  9.        System.arraycopy(array1,0, array2, 0, 100);
  10.     }
  11. }

五、让访问实例内变量的getter/setter方法变成”final”
简单的getter/setter方法应该被置成final,这会告诉编译器,这个方法不会被重载,所以,可以变成”inlined”,例子:

  1. classMAF {
  2.     publicvoidsetSize (int size) {
  3.         _size = size;
  4.     }
  5.     privateint_size;
  6. }

更正:

  1. classDAF_fixed {
  2.     finalpublicvoidsetSize (int size) {
  3.         _size = size;
  4.     }
  5.     privateint_size;
  6. }

六、避免不需要的instanceof操作
如果左边的对象的静态类型等于右边的,instanceof表达式返回永远为true。
例子:

  1. publicclassUISO {
  2.     publicUISO () {}
  3. }
  4. classDogextendsUISO {
  5.     voidmethod (Dogdog, UISO u) {
  6.        Dogd = dog;
  7.        if (d instanceofUISO) //always true.
  8.           System.out.println("Dogis a UISO");
  9.        UISO uiso = u;
  10.        if (uiso instanceofObject)// always true.
  11.           System.out.println("uisois an Object");
  12.     }
  13. }


更正:
删掉不需要的instanceof操作。

  1. lang="as">
  2. class Dog extends UISO{
  3.     void method (){
  4.        Dog d;
  5.        System.out.println ("Dog is anUISO");
  6.        System.out.println ("UISO is anUISO");
  7.     }
  8. }

七、避免不需要的造型操作
所有的类都是直接或者间接继承自Object。同样,所有的子类也都隐含的“等于”其父类。那么,由子类造型至父类的操作就是不必要的了。例子:

  1. classUNC {
  2.     String_id = "UNC";
  3. }
  4. classDogextendsUNC {
  5.     voidmethod () {
  6.        Dogdog = new Dog();
  7.        UNC animal = (UNC)dog;   // not necessary.
  8.        Objecto = (Object)dog;       // notnecessary.
  9.     }
  10. }


更正:

  1. classDogextendsUNC {
  2.     voidmethod () {
  3.        Dogdog = new Dog();
  4.        UNC animal = dog;
  5.        Objecto = dog;
  6.     }
  7. }

八、对于常量字符串,用'String' 代替 'StringBuffer'
常量字符串并不需要动态改变长度。
例子:

  1. publicclassUSC {
  2.     Stringmethod () {
  3.        StringBuffers = new StringBuffer("Hello");
  4.        Stringt = s + "World!";
  5.        returnt;
  6.     }
  7. }

更正:
把StringBuffer换成String,如果确定这个String不会再变的话,这将会减少运行开销提高性能。
九、在字符串相加的时候,使用 ' ' 代替 " ",如果该字符串只有一个字符的话
例子:

  
  1. publicclassSTR {
  2.     publicvoidmethod(Strings) {
  3.        Stringstring= s + "d"   // violation.
  4.        string= "abc" + "d"      //violation.
  5.     }
  6. }

更正:
将一个字符的字符串替换成' '

  1. publicclassSTR {
  2.     publicvoidmethod(Strings) {
  3.        Stringstring= s + 'd'
  4.        string= "abc" + 'd'  
  5.     }
  6. }

尽量别用异常
异常对性能不利。抛出异常首先要创建一个新的对象。Throwable接口的构造函数调用名为fillInStackTrace()的本地(Native)方法,fillInStackTrace()方法检查堆栈,收集调用跟踪信息。只要有异常被抛出,VM就必须调整调用堆栈,因为在处理过程中创建了一个新的对象。异常只能用于错误处理,不应该用来控制程序流程。

Try {} catch()要使用得当。

不要在循环中使用:
Try {
} catch() {
}
应把其放置在最外层。

尽量使用HashMap 和ArrayList ,除非必要,否则不推荐使用HashTable和Vector,后者由于使用同步机制,而导致了性能的开销。











0 0
原创粉丝点击