java程序优化

白色玫瑰 程序猿

时间: 2023-07-11 阅读: 1 字数:22647

{}
一、避免在循环条件中使用复杂表达式 \r\n\r\n在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快。 \r\n\r\n例子: \r\nimport java.util.vector;\r\nclass cel {\r\n void method (vector vector) {\r\n for (int i = 0; i < vect

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

import java.util.vector;
class cel {
   void method (vector vector) {
      for (int i = 0; i < vector.size (); i++)  // violation
         ; // ...
   }
}

<br style=""> 更正:
<br style="">

class cel_fixed {
   void method (vector vector) {
      int size = vector.size ()
      for (int i = 0; i < size; i++)
         ; // ...
   }
}

<br style=""> 二、为'vectors' 和 'hashtables'定义初始大小

<br style=""> jvm为vector扩充大小的时候需要重新创建一个更大的数组,将原原先数组中的内容复制过来,最后,原先的数组再被回收。可见vector容量的扩大是一个颇费时间的事。
<br style=""> 通常,默认的10个元素大小是不够的。你最好能准确的估计你所需要的最佳大小。
<br style=""> <br style=""> 例子:
<br style="">

import java.util.vector;
public class dic {
   public void addobjects (object[] o) {
      // if length > 10, vector needs to expand
      for (int i = 0; i< o.length;i++) {   
         v.add(o);   // capacity before it can add more elements.
      }
   }
   public vector v = new vector();  // no initialcapacity.
}

<br style=""> 更正:
<br style=""> 自己设定初始大小。
<br style="">

   public vector v = new vector(20);  
   public hashtable hash = new hashtable(10);

<br style=""> <br style=""> 参考资料:
<br style=""> dov bulka, "java performance and scalability volume 1: server-side programming
<br style=""> techniques" addison wesley, isbn: 0-201-70429-3 pp.55 – 57
<br style=""> <br style=""> 三、在finally块中关闭stream
<br style=""> <br style=""> 程序中使用到的资源应当被释放,以避免资源泄漏。这最好在finally块中去做。不管程序执行的结果如何,finally块总是会执行的,以确保资源的正确关闭。
<br style="">
<br style=""> 例子:
<br style="">

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) {
      } catch (ioexception e2) {
      }
   }
}

<br style="">
<br style=""> 更正:
<br style=""> 在最后一个catch后添加一个finally块
<br style=""> <br style=""> 参考资料:
<br style=""> peter haggar: "practical java - programming language guide".
<br style=""> addison wesley, 2000, pp.77-79
<br style=""> 四、使用'system.arraycopy ()'代替通过来循环复制数组
<br style=""> <br style=""> 'system.arraycopy ()' 要比通过循环来复制数组快的多。
<br style="">
<br style=""> 例子:
<br style="">

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
      }
   }
}

<br style="">
<br style=""> 更正:
<br style="">

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);
   }
}

<br style="">
<br style=""> 参考资料:
<br style=""> http://www.cs.cmu.edu/~jch/java/speed.html
<br style=""> 五、让访问实例内变量的getter/setter方法变成”final”
<br style=""> <br style=""> 简单的getter/setter方法应该被置成final,这会告诉编译器,这个方法不会被重载,所以,可以变成”inlined”
<br style=""> <br style=""> 例子:
<br style="">

class maf {
   public void setsize (int size) {
       _size = size;
   }
   private int _size;
}

<br style=""> <br style=""> 更正:
<br style="">

class daf_fixed {
   final public void setsize (int size) {
       _size = size;
   }
   private int _size;
}

<br style=""> <br style=""> 参考资料:
<br style=""> warren n. and bishop p. (1999), "java in practice", p. 4-5
<br style=""> addison-wesley, isbn 0-201-36065-9
<br style=""> 六、避免不需要的instanceof操作
<br style=""> <br style=""> 如果左边的对象的静态类型等于右边的,instanceof表达式返回永远为true。
<br style="">
<br style=""> 例子:
<br style="">

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");
   }
}

<br style="">
<br style=""> 更正:
<br style=""> 删掉不需要的instanceof操作。
<br style="">
<br style="">

class dog extends uiso {
   void method () {
      dog d;
      system.out.println ("dog is an uiso");
      system.out.println ("uiso is an uiso");
   }
}

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

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.
   }
}

<br style="">
<br style=""> 更正:
<br style="">

class dog extends unc {
   void method () {
      dog dog = new dog();
      unc animal = dog;
      object o = dog;
   }
}
   

<br style=""> 参考资料:
<br style=""> nigel warren, philip bishop: "java in practice - design styles and idioms
<br style=""> for effective java". addison-wesley, 1999. pp.22-23
<br style=""> 八、如果只是查找单个字符的话,用charat()代替startswith()
<br style=""> <br style=""> 用一个字符作为参数调用startswith()也会工作的很好,但从性能角度上来看,调用用string api无疑是错误的!
<br style="">
<br style=""> 例子:
<br style="">

public class pcts {
   private void method(string s) {
      if (s.startswith("a")) { // violation
         // ...
      }
   }
}

<br style="">
<br style=""> 更正
<br style=""> 将'startswith()' 替换成'charat()'.
<br style="">

public class pcts {
   private void method(string s) {
      if ('a' == s.charat(0)) {
         // ...
      }
   }
}

<br style="">
<br style=""> 参考资料:
<br style=""> dov bulka, "java performance and scalability volume 1: server-side programming
<br style=""> techniques" addison wesley, isbn: 0-201-70429-3
<br style=""> 九、使用移位操作来代替'a / b'操作
<br style=""> <br style=""> "/"是一个很“昂贵”的操作,使用移位操作将会更快更有效。
<br style=""> <br style=""> 例子:
<br style="">

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;
   }
}

<br style=""> <br style=""> 更正:
<br style="">

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;      // 不能转换成位移操作
   }
}

<br style=""> <br style=""> 十、使用移位操作代替'a * b'
<br style=""> <br style=""> 同上。
<br style=""> [i]但我个人认为,除非是在一个非常大的循环内,性能非常重要,而且你很清楚你自己在做什么,方可使用这种方法。否则提高性能所带来的程序晚读性的降低将是不合算的。
<br style=""> <br style=""> 例子:
<br style="">

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;
   }
}

<br style=""> <br style=""> 更正:
<br style="">

package opt;
public class smul {
   public void calculate(int a) {
      int mul = a << 2;  
      int mul2 = a << 3;
      int temp = a * 3;      // 不能转换
   }
}

<br style=""> 十一、在字符串相加的时候,使用 ' ' 代替 " ",如果该字符串只有一个字符的话
<br style=""> <br style=""> <br style=""> 例子:
<br style="">

public class str {
   public void method(string s) {
      string string = s + "d"  // violation.
      string = "abc" + "d"     // violation.
   }
}

<br style=""> 更正:
<br style=""> 将一个字符的字符串替换成' '
<br style="">

public class str {
   public void method(string s) {
      string string = s + 'd'
      string = "abc" + 'd'   
   }
}

<br style=""> 十二、不要在循环中调用synchronized(同步)方法
<br style=""> <br style=""> 方法的同步需要消耗相当大的资料,在一个循环中调用它绝对不是一个好主意。
<br style=""> <br style=""> 例子:
<br style="">

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);
}

<br style=""> <br style=""> 更正:
<br style=""> 不要在循环体中调用同步方法,如果必须同步的话,推荐以下方式:
<br style="">

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);
}

<br style=""> <br style=""> 十三、将try/catch块移出循环
<br style=""> <br style=""> 把try/catch块放入循环体内,会极大的影响性能,如果编译jit被关闭或者你所使用的是一个不带jit的jvm,性能会将下降21%之多!
<br style="">
<br style=""> 例子:
<br style="">

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;
}

<br style="">
<br style=""> 更正:
<br style=""> 将try/catch块移出循环
<br style="">

 void method (fileinputstream fis) {
      try {
         for (int i = 0; i < size; i++) {
            _sum += fis.read();
         }
      } catch (exception e) {}
   }

<br style="">
<br style=""> 参考资料:
<br style=""> peter haggar: "practical java - programming language guide".
<br style=""> addison wesley, 2000, pp.81 – 83
<br style=""> <br style=""> 十四、对于boolean值,避免不必要的等式判断
<br style=""> <br style=""> 将一个boolean值与一个true比较是一个恒等操作(直接返回该boolean变量的值). 移走对于boolean的不必要操作至少会带来2个好处:
<br style=""> 1)代码执行的更快 (生成的字节码少了5个字节);
<br style=""> 2)代码也会更加干净 。
<br style=""> <br style=""> 例子:
<br style="">

public class ueq
{
   boolean method (string string) {
      return string.endswith ("a") == true;   // violation
   }
}

<br style=""> <br style=""> 更正:
<br style="">

class ueq_fixed
{
   boolean method (string string) {
      return string.endswith ("a");
   }
}

<br style=""> 十五、对于常量字符串,用'string' 代替 'stringbuffer'
<br style=""> <br style=""> 常量字符串并不需要动态改变长度。
<br style=""> 例子:
<br style="">

public class usc {
   string method () {
      stringbuffer s = new stringbuffer ("hello");
      string t = s + "world!";
      return t;
   }
}

<br style=""> <br style=""> 更正:
<br style=""> 把stringbuffer换成string,如果确定这个string不会再变的话,这将会减少运行开销提高性能。
<br style=""> <br style=""> 十六、用'stringtokenizer' 代替 'indexof()' 和'substring()'
<br style=""> <br style=""> 字符串的分析在很多应用中都是常见的。使用indexof()和substring()来分析字符串容易导致 stringindexoutofboundsexception。而使用stringtokenizer类来分析字符串则会容易一些,效率也会高一些。
<br style=""> <br style=""> 例子:
<br style="">

public class ust {
   void parsestring(string string) {
      int index = 0;
      while ((index = string.indexof(".", index)) != -1) {
         system.out.println (string.substring(index, string.length()));
      }
   }
}

<br style=""> 参考资料:
<br style=""> graig larman, rhett guthrie: "java 2 performance and idiom guide"
<br style=""> prentice hall ptr, isbn: 0-13-014260-3 pp. 282 – 283
<br style=""> <br style=""> 十七、使用条件操作符替代"if (cond) return; else return;" 结构
<br style=""> <br style=""> 条件操作符更加的简捷
<br style=""> 例子:
<br style="">

public class if {
   public int method(boolean isdone) {
      if (isdone) {
         return 0;
      } else {
         return 10;
      }
   }
}

<br style=""> <br style=""> 更正:
<br style="">

public class if {
   public int method(boolean isdone) {
      return (isdone ? 0 : 10);
   }
}

<br style=""> 十八、使用条件操作符代替"if (cond) a = b; else a = c;" 结构
<br style=""> <br style=""> 例子:
<br style="">

public class ifas {
   void method(boolean istrue) {
      if (istrue) {
         _value = 0;
      } else {
         _value = 1;
      }
   }
   private int _value = 0;
}

<br style=""> <br style=""> 更正:
<br style="">

public class ifas {
   void method(boolean istrue) {
      _value = (istrue ? 0 : 1);      // compact expression.
   }
   private int _value = 0;
}

<br style=""> 十九、不要在循环体中实例化变量
<br style=""> <br style=""> 在循环体中实例化临时变量将会增加内存消耗
<br style=""> <br style=""> 例子:
<br style="">

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);
      }
   }
}

<br style="">
<br style=""> 更正:
<br style=""> 在循环体外定义变量,并反复使用
<br style="">

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);
      }
   }
}

<br style=""> <br style=""> 二十、确定 stringbuffer的容量
<br style=""> <br style=""> stringbuffer的构造器会创建一个默认大小(通常是16)的字符数组。在使用中,如果超出这个大小,就会重新分配内存,创建一个更大的数组,并将原先的数组复制过来,再丢弃旧的数组。在大多数情况下,你可以在创建stringbuffer的时候指定大小,这样就避免了在容量不够的时候自动增长,以提高性能。
<br style=""> <br style=""> 例子:
<br style="">

public class rsbc {
   void method () {
      stringbuffer buffer = new stringbuffer(); // violation
      buffer.append ("hello");
   }
}

<br style="">
<br style=""> 更正:
<br style=""> 为stringbuffer提供寝大小。
<br style="">

public class rsbc {
   void method () {
      stringbuffer buffer = new stringbuffer(max);
      buffer.append ("hello");
   }
   private final int max = 100;
}

<br style="">
<br style=""> 参考资料:
<br style=""> dov bulka, "java performance and scalability volume 1: server-side programming
<br style=""> techniques" addison wesley, isbn: 0-201-70429-3 p.30 – 31
<br style=""> <br style=""> 二十一、尽可能的使用栈变量
<br style=""> <br style=""> 如果一个变量需要经常访问,那么你就需要考虑这个变量的作用域了。static? local?还是实例变量?访问静态变量和实例变量将会比访问局部变量多耗费2-3个时钟周期。
<br style="">
<br style=""> 例子:
<br style="">

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;
}    

<br style="">
<br style=""> 更正:
<br style=""> 如果可能,请使用局部变量作为你经常访问的变量。
<br style=""> 你可以按下面的方法来修改getsum()方法:
<br style="">

void getsum (int[] values) {
   int sum = _sum;  // temporary local variable.
   for (int i=0; i < value.length; i++) {
      sum += value[i];
   }
   _sum = sum;
}

<br style="">
<br style=""> 参考资料:
<br style=""> peter haggar: "practical java - programming language guide".
<br style=""> addison wesley, 2000, pp.122 – 125
<br style=""> <br style=""> 二十二、不要总是使用取反操作符(!)
<br style=""> <br style=""> 取反操作符(!)降低程序的可读性,所以不要总是使用。
<br style=""> <br style=""> 例子:
<br style="">

public class dun {
   boolean method (boolean a, boolean b) {
      if (!a)
         return !a;
      else
         return !b;
   }
}

<br style=""> <br style=""> 更正:
<br style=""> 如果可能不要使用取反操作符(!)
<br style=""> 二十三、与一个接口 进行instanceof操作
<br style=""> <br style=""> 基于接口的设计通常是件好事,因为它允许有不同的实现,而又保持灵活。只要可能,对一个对象进行instanceof操作,以判断它是否某一接口要比是否某一个类要快。
<br style=""> <br style=""> 例子:
<br style="">

public class insof {
   private void method (object o) {
      if (o instanceof interfacebase) { }  // better
      if (o instanceof classbase) { }   // worse.
   }
}

class classbase {}
interface interfacebase {}

原文地址:https://blog.csdn.net/htoue456/article/details/11775951?ops_request_misc=&request_id=2ed39bf585544a58bf5df53eee7d3eca&biz_id=&utm_medium=distribute.pc_search_result.none-task-blog-2~all~koosearch~default-13-11775951-null-null.142^v88^insert_down28v1,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96

本文章网址:https://www.sjxi.cn/detil/cc300f60575e453582073fb7285f61e4

最新评论

当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号

×

(穷逼博主)在线接单

QQ: 1164453243

邮箱: abcdsjx@126.com

前端项目代做
前后端分离
Python 爬虫脚本
Java 后台开发
各种脚本编写
服务器搭建
个人博客搭建
Web 应用开发
Chrome 插件编写
Bug 修复