Java performance optimization universal articles

xiaoxiao2021-03-06  40

I. The issue of "General Articles" discussed is suitable for most Java applications. 1.1 Do not create a class with a new keyword to create an instance of a class with a new keyword, all constructor in the constructor is 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 ();} After the improved code uses the clone () method, as shown below:

Private static credit basecredit = new credit (); public static credit getnewcredit () {return () Basecredit.clone ();} The above ideas is also useful for array processing. 1.2 Using Non-Block I / O Local JDK does not support non-blocking 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 of abnormal abnormalities 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 the initialization variable 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 The Double variable is set to 0.0, and the logical 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 class of Final modifiers with the Final modifier class is 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. 1.7 Multiplication and division Consider the following code: for (VAL = 0; Val <100000; VAL = 5) {alterx = val * 8; myresult = val * 2;} Replacement multiplication with shift operation can greatly improve performance . Below is the modified code:

转载请注明原文地址:https://www.9cbs.com/read-75564.html

New Post(0)