Performance optimized code must be remembered that the law optimization -15 bar

3, code optimization

Optimization, not only in the optimization of the operating environment, you need to do to optimize the code itself, there is a performance problem if the code itself, then other aspects of how optimization can not achieve optimal.

3.1, as far as possible to use local variables

Delivery method call parameters and temporary variables created in the call are stored in the stack faster, other variables, such as static variables, instance variables, etc., are created on the heap, slower speed. In addition, the stack variables created with the method of operation is completed, the content is gone, no additional garbage collection.

3.2, to minimize duplication of the calculation variables

A clear concept, method calls, even if the method is only one sentence, is also consumed. So, for example, the following operations:

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

Recommended alternatives:
Here Insert Picture Description

In this way, list.size () a lot of time, it reduces a lot of consumption.

3.3, lazy loading strategy as far as possible, that is only created when needed

Here Insert Picture Description

3.4, an exception should not be used to control program flow

Abnormal detrimental to performance. An exception is thrown first to create a new object, Throwable constructor function call interface called the fillInStackTrace()local synchronization method, fillInStackTrace () method to check the stack to collect information call tracking. As long as there is an exception is thrown, Java virtual machine must adjust the call stack, because a new object is created in the process. An exception can only be used for error handling, it should not be used to control program flow.

3.5, do not declare the array as public static final

Because this is meaningless, it just defines a reference for the static final, or the contents of the array can be freely changed, the array is declared as a public security vulnerability, which means that the array can be changed outside the class.

3.6, do not create some objects do not use, do not import some classes do not use

This is meaningless, if the code appears in " The value of the local variable i is not used", " The import java.util is never used", then delete these useless content

3.7, the program is running to avoid using reflection

Java reflection is to provide the user with a very powerful, powerful often means inefficient. In particular, frequent use is not recommended to use reflection, especially in the invoke method Method of the program is running.

If indeed it is necessary a suggestive approach is reflected those classes need to load at the time of project start by reflecting instantiate an object and placed in memory.

3.8, using database connection pooling and thread pooling

Both pools are objects for reuse, frequently the former avoid opening and closing connections, which avoids creating and destroying threads frequently.

3.9, as specified length initialization container

Specifies the length of the container as initialization, such as: new ArrayList <> (10); new HashMap <> (32); avoiding insufficient length of the vessel, expansion of the performance cost.

3.10, ArrayList random traversal fast, LinkedList add remove fast

3.11, use the Entry traverse Map

Here Insert Picture Description
Avoid using this way:
Here Insert Picture Description

3.12 Do not manually call System.gc ();

3.13, String minimize the use of regular expressions

Regular expressions although powerful, but less efficient, unless there is a need, or as little as possible.

  • replace() Does not support regular
  • replaceAll()It supports regular
    if only the recommended replacement character replace ().

3.14, to pay attention to the level of the output log

// 当 前 的 日 志 级 别 是 error 
LOGGER.info("保存出错!" + user);

3.15, to close the resource () is recommended operated separately

Here Insert Picture Description

Released 1064 original articles · won praise 888 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/103990883