Home > Java > javaTutorial > Share 35 Java code performance optimization methods

Share 35 Java code performance optimization methods

怪我咯
Release: 2017-04-05 15:58:40
Original
1636 people have browsed it

Preface

Code optimization is a very important topic. Some people may think it is useless. What are the small things that can be modified? What impact will the modification or not have on the running efficiency of the code? I think about this question like this, just like a whale in the sea, is it useful for it to eat a small shrimp? It was useless, but after eating more shrimps, the whale was full. The same is true for code optimization. If the project focuses on launching without bugs as soon as possible, then you can focus on the big and let go of the small at this time, and the details of the code do not need to be refined; but if there is enough time to develop and maintain the code, you must consider every aspect at this time. There are details that can be optimized. The accumulation of small optimization points one by one will definitely improve the running efficiency of the code.

The goals of code optimization are:

1. Reduce the size of the code

2. Improve the efficiency of code operation

Details of code optimization

1. Try to specify the final modifier of classes and methods.

Classes with final modifiers cannot be derived. In the Java core API, there are many examples of final applications, such as java.lang.String, where the entire class is final. Specifying the final modifier for a class prevents the class from being inherited, and specifying the final modifier for a method prevents the method from being overridden. If a class is designated as final, all methods of the class are final. The Java compiler will look for opportunities to inline all final methods. Inlining plays a significant role in improving Java running efficiency. For details, see Java runtime optimization. This can improve performance by an average of 50%.

2. Try to reuse objects

Especially when using String objects, StringBuilder/StringBuffer should be used instead when string connections occur. Since the Java virtual machine not only spends time generating objects, it may also need to spend time garbage collecting and processing these objects in the future. Therefore, generating too many objects will have a great impact on the performance of the program.

3. Use local variables as much as possible

The parameters passed when calling the method and the temporary variables created during the call are saved in the stack faster. Other variables, such as static variables and instances Variables, etc., are all created in the heap, which is slower. In addition, the contents of variables created in the stack are gone as the method ends, and no additional garbage collection is required.

4. Close the stream in a timely manner

During the Java programming process, be careful when performing database connections and I/O stream operations. After use, close the stream in time to release resources. Because operating these large objects will cause a lot of system overhead, a little carelessness will lead to serious consequences.

5. Minimize repeated calculations of variables

Clear a concept. Calling a method, even if there is only one statement in the method, is costly, including when creating a stack frame and calling a method. Protect the scene, restore the scene when the method is called, etc. So for example, the following operation:

for (int i = 0; i < list.size(); i++)

{...}
Copy after login

is recommended to be replaced by:

for (int i = 0, int length = list.size(); i < length; i++)
{...}
Copy after login

In this way, when list.size() is very large, a lot of consumption is reduced

6 , Try to adopt a lazy loading strategy, that is, create

only when needed. For example:

String str = "aaa";
if (i == 1){
 list.add(str);
}
Copy after login

It is recommended to replace it with:

if (i == 1){
    String str = "aaa";
    list.add(str);
}
Copy after login

7. Use exceptions with caution

Exceptions are bad for performance. To throw an exception, you must first create a new object. The constructor of the Throwable interface calls the local synchronization method named fillInStackTrace(). The fillInStackTrace() method checks the stack and collects call tracking information. Whenever an exception is thrown, the Java virtual machine must adjust the call stack because a new object is created during processing. Exceptions should only be used for error handling and should not be used to control program flow.

8. Do not use try...catch... in a loop. It should be placed in the outermost layer

unless it is absolutely necessary. If you write this without any reason, as long as your leader is more senior and has more obsessive-compulsive disorder, he will most likely scold you for writing such garbage code

9. If you can estimate the code to be added Content length, specify the initial length for the underlying collections and tool classes implemented in arrays

such as ArrayList, LinkedLlist, StringBuilder, StringBuffer, HashMap, HashSet, etc., taking StringBuilder as an example:

( 1) StringBuilder() // Allocates 16 characters of space by default

(2) StringBuilder(int size) // Allocates size characters of space by default

(3) StringBuilder(String str ) // By default, 16 characters + str.length() character space are allocated

You can set its initialization capacity through the class (here refers to not only the above StringBuilder), so that you can obviously Improve performance. For example, StringBuilder, length represents the number of characters that the current StringBuilder can hold. Because when StringBuilder reaches its maximum capacity, it will increase its capacity to 2 times its current capacity plus 2. Whenever StringBuilder reaches its maximum capacity, it has to create a new character array and then replace the old characters with The contents of the array are copied into a new character array - this is a very performance-intensive operation. Just imagine, if it can be estimated that the character array will store approximately 5000 characters without specifying the length, the closest power of 2 to 5000 is 4096, and the 2 will be added for each expansion, then:

(1)在4096 的基础上,再申请8194个大小的字符数组,加起来相当于一次申请了12290个大小的字符数组,如果一开始能指定5000个大小的字符数组,就节省了一倍以上的空间

(2)把原来的4096个字符拷贝到新的的字符数组中去

这样,既浪费内存空间又降低代码运行效率。所以,给底层以数组实现的集合、工具类设置一个合理的初始化容量是错不了的,这会带来立竿见影的效果。但是,注意,像HashMap这种是以数组+链表实现的集合,别把初始大小和你估计的大小设置得一样,因为一个table上只连接一个对象的可能性几乎为0。初始大小建议设置为2的N次幂,如果能估计到有2000个元素,设置成new HashMap(128)、new HashMap(256)都可以。

10、当复制大量数据时,使用System.arraycopy()命令

11、乘法和除法使用移位操作

例如:

for (val = 0; val < 100000; val += 5){
    a = val * 8;
    b = val / 2;
}
Copy after login

用移位操作可以极大地提高性能,因为在计算机底层,对位的操作是最方便、最快的,因此建议修改为:

for (val = 0; val < 100000; val += 5){
    a = val << 3;
    b = val >> 1;
}
Copy after login

移位操作虽然快,但是可能会使代码不太好理解,因此最好加上相应的注释。

12、循环内不要不断创建对象引用

例如:

for (int i = 1; i <= count; i++){
    Object obj = new Object();
}
Copy after login

这种做法会导致内存中有count份Object对象引用存在,count很大的话,就耗费内存了,建议为改为:

Object obj = null;
for (int i = 0; i <= count; i++) {
    obj = new Object();
}
Copy after login

这样的话,内存中只有一份Object对象引用,每次new Object()的时候,Object对象引用指向不同的Object罢了,但是内存中只有一份,这样就大大节省了内存空间了。

13、基于效率和类型检查的考虑,应该尽可能使用array,无法确定数组大小时才使用ArrayList

14、尽量使用HashMap、ArrayList、StringBuilder,除非线程安全需要,否则不推荐使用Hashtable、Vector、StringBuffer,后三者由于使用同步机制而导致了性能开销

15、不要将数组声明为public static final

因为这毫无意义,这样只是定义了引用为static final,数组的内容还是可以随意改变的,将数组声明为public更是一个安全漏洞,这意味着这个数组可以被外部类所改变

16、尽量在合适的场合使用单例

使用单例可以减轻加载的负担、缩短加载的时间、提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面:

(1)控制资源的使用,通过线程同步来控制资源的并发访问

(2)控制实例的产生,以达到节约资源的目的

(3)控制数据的共享,在不建立直接关联的条件下,让多个不相关的进程或线程之间实现通信

17、尽量避免随意使用静态变量

要知道,当某个对象被定义为static的变量所引用,那么gc通常是不会回收这个对象所占有的堆内存的,如:

public class A{ 
   private static B b = new B();
}
Copy after login

此时静态变量b的生命周期与A类相同,如果A类不被卸载,那么引用B指向的B对象会常驻内存,直到程序终止

18、及时清除不再需要的会话

为了清除不再活动的会话,许多应用服务器都有默认的会话超时时间,一般为30分钟。当应用服务器需要保存更多的会话时,如果内存不足,那么操作系统会把部分数据转移到磁盘,应用服务器也可能根据MRU(最近最频繁使用)算法把部分不活跃的会话转储到磁盘,甚至可能抛出内存不足的异常。如果会话要被转储到磁盘,那么必须要先被序列化,在大规模集群中,对对象进行序列化的代价是很昂贵的。因此,当会话不再需要时,应当及时调用HttpSession的invalidate()方法清除会话。

19、实现RandomAccess接口的集合比如ArrayList,应当使用最普通的for循环而不是foreach循环来遍历

这是JDK推荐给用户的。JDK API对于RandomAccess接口的解释是:实现RandomAccess接口用来表明其支持快速随机访问,此接口的主要目的是允许一般的算法更改其行为,从而将其应用到随机或连续访问列表时能提供良好的性能。实际经验表明,实现RandomAccess接口的类实例,假如是随机访问的,使用普通for循环效率将高于使用foreach循环;反过来,如果是顺序访问的,则使用Iterator会效率更高。可以使用类似如下的代码作判断:

if (list instanceof RandomAccess){
for (int i = 0; i < list.size(); i++){}

}else{
Iterator<?> iterator = list.iterable();
while (iterator.hasNext()){
    iterator.next()
}
}
Copy after login

foreach循环的底层实现原理就是迭代器Iterator,参见Java语法糖1:可变长度参数以及foreach循环原理。所以后半句”反过来,如果是顺序访问的,则使用Iterator会效率更高”的意思就是顺序访问的那些类实例,使用foreach循环去遍历。

20、使用同步代码块替代同步方法

这点在多线程模块中的synchronized锁方法块一文中已经讲得很清楚了,除非能确定一整个方法都是需要进行同步的,否则尽量使用同步代码块,避免对那些不需要进行同步的代码也进行了同步,影响了代码执行效率。

21、将常量声明为static final,并以大写命名

这样在编译期间就可以把这些内容放入常量池中,避免运行期间计算生成常量的值。另外,将常量的名字以大写命名也可以方便区分出常量与变量

22、不要创建一些不使用的对象,不要导入一些不使用的类

这毫无意义,如果代码中出现”The value of the local variable i is not used”、”The import java.util is never used”,那么请删除这些无用的内容

23、程序运行过程中避免使用反射

关于,请参见反射。反射是Java提供给用户一个很强大的功能,功能强大往往意味着效率不高。不建议在程序运行过程中使用尤其是频繁使用反射机制,特别是Method的invoke方法,如果确实有必要,一种建议性的做法是将那些需要通过反射加载的类在项目启动的时候通过反射实例化出一个对象并放入内存—-用户只关心和对端交互的时候获取最快的响应速度,并不关心对端的项目启动花多久时间。

24、使用数据库连接池和线程池

这两个池都是用于重用对象的,前者可以避免频繁地打开和关闭连接,后者可以避免频繁地创建和销毁线程

25、使用带缓冲的输入输出流进行IO操作

带缓冲的输入输出流,即BufferedReader、BufferedWriter、BufferedInputStream、BufferedOutputStream,这可以极大地提升IO效率

26、顺序插入和随机访问比较多的场景使用ArrayList,元素删除和中间插入比较多的场景使用LinkedList

这个,理解ArrayList和LinkedList的原理就知道了

27、不要让public方法中有太多的形参

public方法即对外提供的方法,如果给这些方法太多形参的话主要有两点坏处:

1、违反了面向对象的编程思想,Java讲求一切都是对象,太多的形参,和面向对象的编程思想并不契合

2、参数太多势必导致方法调用的出错概率增加

至于这个”太多”指的是多少个,3、4个吧。比如我们用JDBC写一个insertStudentInfo方法,有10个学生信息字段要插如Student表中,可以把这10个参数封装在一个实体类中,作为insert方法的形参

28、字符串变量和字符串常量equals的时候将字符串常量写在前面

这是一个比较常见的小技巧了,如果有以下代码:

String str = "123";
if (str.equals("123")) {
...
}
Copy after login

建议修改为:

String str = "123";
if ("123".equals(str)){
...
}
Copy after login

这么做主要是可以避免空指针异常

29、请知道,在java中if (i == 1)和if (1 == i)是没有区别的,但从阅读习惯上讲,建议使用前者

平时有人问,”if (i == 1)”和”if (1== i)”有没有区别,这就要从C/C++讲起。

在C/C++中,”if (i == 1)”判断条件成立,是以0与非0为基准的,0表示false,非0表示true,如果有这么一段代码:

int i = 2;
if (i == 1){
...
}else{
...
}
Copy after login

C/C++判断”i==1″不成立,所以以0表示,即false。但是如果:

int i = 2;
if (i = 1) { ... }else{ ... }
Copy after login

万一程序员一个不小心,把”if (i == 1)”写成”if (i = 1)”,这样就有问题了。在if之内将i赋值为1,if判断里面的内容非0,返回的就是true了,但是明明i为2,比较的值是1,应该返回的false。这种情况在C/C++的开发中是很可能发生的并且会导致一些难以理解的错误产生,所以,为了避免开发者在if语句中不正确的赋值操作,建议将if语句写为:

int i = 2;
if (1 == i) { ... }else{ ... }
Copy after login

这样,即使开发者不小心写成了”1 = i”,C/C++编译器也可以第一时间检查出来,因为我们可以对一个变量赋值i为1,但是不能对一个常量赋值1为i。

但是,在Java中,C/C++这种”if (i = 1)”的语法是不可能出现的,因为一旦写了这种语法,Java就会编译报错”Type mismatch: cannot convert from int to boolean”。但是,尽管Java的”if (i == 1)”和”if (1 == i)”在语义上没有任何区别,但是从阅读习惯上讲,建议使用前者会更好些。

30、不要对数组使用toString()方法

看一下对数组使用toString()打印出来的是什么:

public static void main(String[] args){
    int[] is = new int[]{1, 2, 3};
    System.out.println(is.toString());
}
Copy after login

结果是:

[[email protected]
Copy after login

本意是想打印出数组内容,却有可能因为数组引用is为空而导致空指针异常。不过虽然对数组toString()没有意义,但是对集合toString()是可以打印出集合里面的内容的,因为集合的父类AbstractCollections重写了Object的toString()方法。

31、不要对超出范围的基本数据类型做向下强制转型

这绝不会得到想要的结果:

public static void main(String[] args){ 
    long l = 12345678901234L;
    int i = (int)l;
    System.out.println(i);
}
Copy after login

我们可能期望得到其中的某几位,但是结果却是:

1942892530

解释一下。Java中long是8个字节64位的,所以12345678901234在计算机中的表示应该是:

0000 0000 0000 0000 0000 1011 0011 1010 0111 0011 1100 1110 0010 1111 1111 0010

一个int型数据是4个字节32位的,从低位取出上面这串二进制数据的前32位是:

0111 0011 1100 1110 0010 1111 1111 0010

这串二进制表示为十进制1942892530,所以就是我们上面的控制台上输出的内容。从这个例子上还能顺便得到两个结论:

1、整型默认的数据类型是int,long l = 12345678901234L,这个数字已经超出了int的范围了,所以最后有一个L,表示这是一个long型数。顺便,浮点型的默认类型是double,所以定义float的时候要写成”"float f = 3.5f”

2、接下来再写一句”int ii = l + i;”会报错,因为long + int是一个long,不能赋值给int

32、公用的集合类中不使用的数据一定要及时remove掉

如果一个集合类是公用的(也就是说不是方法里面的属性),那么这个集合里面的元素是不会自动释放的,因为始终有引用指向它们。所以,如果公用集合里面的某些数据不使用而不去remove掉它们,那么将会造成这个公用集合不断增大,使得系统有内存泄露的隐患。

33、把一个基本数据类型转为字符串,基本数据类型.toString()是最快的方式、String.valueOf(数据)次之、数据+”"最慢

把一个基本数据类型转为一般有三种方式,我有一个Integer型数据i,可以使用i.toString()、String.valueOf(i)、i+”"三种方式,三种方式的效率如何,看一个测试:

public static void main(String[] args){ 
    int loopTime = 50000;
    Integer i = 0; 
    long startTime = System.currentTimeMillis(); 
    for (int j = 0; j < loopTime; j++){
        String str = String.valueOf(i);
    }

    System.out.println("String.valueOf():" +
     (System.currentTimeMillis() - startTime) + "ms");
    startTime = System.currentTimeMillis(); 
     for (int j = 0; j < loopTime; j++){
        String str = i.toString();
    }

    System.out.println("Integer.toString():" +
      (System.currentTimeMillis() - startTime) + "ms");
    startTime = System.currentTimeMillis(); 
      for (int j = 0; j < loopTime; j++){
    
    String str = i + "";
    
    }

    System.out.println("i + \"\":" + 
      (System.currentTimeMillis() - startTime) + "ms");

}
Copy after login

运行结果为:

String.valueOf():11ms Integer.toString():5ms i + "":25ms
Copy after login

所以以后遇到把一个基本数据类型转为String的时候,优先考虑使用toString()方法。至于为什么,很简单:

1、String.valueOf()方法底层调用了Integer.toString()方法,但是会在调用前做空判断

2、Integer.toString()方法就不说了,直接调用了

3、i + “”底层使用了StringBuilder实现,先用append方法拼接,再用toString()方法获取字符串

三者对比下来,明显是2最快、1次之、3最慢

34、使用最有效率的方式去遍历Map

遍历Map的方式有很多,通常场景下我们需要的是遍历Map中的Key和Value,那么推荐使用的、效率最高的方式是:

public static void main(String[] args){
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("111", "222");
Set<Map.Entry<String, String>> entrySet = hm.entrySet();
Iterator<Map.Entry<String, String>> iter = entrySet.iterator();
while (iter.hasNext()){
    Map.Entry<String, String> entry = iter.next();
    System.out.println(entry.getKey() 
    + "\t" + entry.getValue());
}
}
Copy after login

如果你只是想遍历一下这个Map的key值,那用”Set keySet = hm.keySet();”会比较合适一些

35、对资源的close()建议分开操作

意思是,比如我有这么一段代码:

try{
    XXX.close();
    YYY.close();
}catch (Exception e){
...
}
Copy after login

建议修改为:

try{
     XXX.close();
}catch (Exception e) {
     ... 
}
try{
  YYY.close();
}catch (Exception e) {
    ...
}
Copy after login

虽然有些麻烦,却能避免资源泄露。我们想,如果没有修改过的代码,万一XXX.close()抛异常了,那么就进入了cath块中了,YYY.close()不会执行,YYY这块资源就不会回收了,一直占用

The above is the detailed content of Share 35 Java code performance optimization methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template