Summary: The resources (memory, CPU time, network bandwidth, etc.) that can be utilized are limited, optimized to complete the predetermined tasks with as little resource as possible. Optimization usually contains two aspects: reduce the volume of the code, improve the operating efficiency of the code. This paper discussed how mainly how to improve the efficiency of code.
First, universal articles
The issue of "universal article" is suitable for most Java applications.
1.1 Examples of creating classes without NEW keywords
When you create an instance of a class with a new keyword, all constructors in the constructor chain are automatically called. But if an object implements a Cloneable interface, we can call its clone () method. The clone () method does not call any class constructor.
In the case of design mode (Design Pattern), if you create an object with a Factory mode, you can use the Clone () method to create a new object instance is very simple. For example, the following is a typical implementation of the Factory mode:
Public static credit getnewcredit () {return new credit ();
The improved code uses the clone () method, as shown below:
Private static credit basecredit = new credit (); public static credit getnewcredit () {return (credit) Basecredit.clone ();
The above ideas is equally useful for array processing.
1.2 use non-blocking I / O
The low version of JDK does not support Non-block I / O APIs. In order to avoid I / O blocking, some applications use a way to create a large number of threads (in better case, using a buffer pool). This technology can be seen in many applications that must support concurrent I / O streams, such as web servers, quotes, and auction applications. However, creating a Java thread requires considerable overhead.
JDK 1.4 introduces a non-blocking I / O library (Java.nio). If the application requires a version earlier JDK, there is a package that supports non-blocking I / O.
1.3 cautious use
Exception is unfavorable. Throw an exception first to create a new object. The constructor of the Throwable interface calls the local (Native) method called FillInstackTrace (), and the FillInstackTrace () method checks the stack to collect the trace information. As long as there is an abnormality being thrown, the VM must adjust the calling stack because a new object is created during the processing.
An exception can only be used for error processing, and should not be used to control the program process.
1.4 Do not repeat initialization variables
By default, when the class constructor is called, Java initializes the variable into a determined value: all objects are set to NULL, integer variables (byte, short, int, long) set to 0, float and double variables set to 0.0, the logic value is set to false. This should especially note when a class is derived from another class, because all constructors in the constructor chain are automatically called when creating an object with the New keyword.
1.5 Try to specify the Final modifier class
Classes with Final modifiers are not born. In the Java core API, there are many examples of Final, such as java.lang.string. Specifying Final for String class to prevent people from overwriting the Length () method.
Also, if you specify a class for Final, all methods of this class are Final. The Java compiler looks for all the Final methods of inline (this and the specific compiler implementation). This move enables performance average by 50%.
1.6 Try to use the local variable calling method to transfer the parameters and the temporary variables created in the call are saved in the stack (STACK), the speed is faster. Other variables such as static variables, example variables, etc. are created in the heap, slower speed. In addition, dependent on specific compiler / JVM, local variables may also be further optimized. See "Using Stack Variables as much as possible."
1.7 multiplication and division
Consider the following code:
FOR (VAL = 0; VAL <100000; VAL = 5) {alterx = val * 8; myresult = val * 2;}
The replacement of multiplication operation can greatly improve performance. Below is the modified code:
FOR (VAL = 0; VAL <100000; VAL = 5) {alterx = val << 3; MyResult = VAL << 1;}
The modified code is no longer multiplied by 8 operation, but is used to switch to the equivalent left shift 3-bit operation, and one bit per left is equivalent to multiplying 2. Accordingly, the right shift 1 bit operation corresponds to division by 2. It is worth mentioning that although the shift is fast, it may make the code difficult to understand, so it is best to add some comments.