Summary of Java Program Performance optimization

Source: Internet
Author: User

1. Avoid using complex expressions in loop conditions

In the case of No compilation optimization, in the loop, the loop condition is evaluated repeatedly, and if a complex expression is not used, the
The program will run faster if the ring condition value is not changed.
Example:

1 Import java.util.vector; 2 class cel {3void  method (vector vector) {4 for (int   violation5//  ... 6 }7 }

Correct:

1 class cel_fixed {2void  method (vector vector) {3int size =  Vector.size ()4 for (int i = 0; i < size; i++)5//  ... 6 }7 }

2. Define the initial size for ' vectors ' and ' hashtables '

When the JVM expands the size of the vector, it needs to recreate a larger array, copying the contents from the original array
, and finally, the original array is recycled. The expansion of vector capacity is a time-consuming matter.
Typically, the default size of 10 elements is not enough. You'd better get an accurate estimate of the best size you need.

3. Close the stream in the finally block

The resources used in the program should be freed to avoid resource leaks. This is best done in the finally block. Regardless of program execution
, the finally block is always executed to ensure that the resource is closed correctly.

4. use ' system.arraycopy () ' Instead of passing to iterate over the array

' System.arraycopy () ' is much faster than copying an array by looping.

5. the Getter/setter method that accesses the variable in the instance becomes "final"

The simple Getter/setter method should be set to final, which tells the compiler that this method is not overloaded, so you can
To become "inlined."

6. Avoid unwanted instanceof operations

7. single character using char instead of string, e.g. Indexof,startswith ()

8. Replace the ' A/b ' operation with a shift operation

"/" is a very "expensive" operation, and using a shift operation will be quicker and more efficient.
Example:

1  Public classSdiv {2  Public Static Final intnum = 16;3  Public voidCalculateinta) {4 intdiv = A/4;5 //should is replaced with "a >> 2".6 intDiv2 = A/8;7 //should is replaced with "a >> 3".8 inttemp = A/3;9 }Ten}

Correct:

1  Public classSdiv {2  Public Static Final intnum = 16;3  Public voidCalculateinta) {4 intdiv = a >> 2;5 intDiv2 = a >> 3;6 inttemp = A/3;7 //cannot convert to displacement operation8 }9}

9. Replace ' A * b ' with a shift operation.

[I] but I personally think that unless it is within a very large cycle, performance is very important and you know exactly what you are doing
, you can use this method. Otherwise, it would be uneconomical to reduce the late reading of the program by improving performance.
Example:

1  Public classSmul {2  Public voidCalculateinta) {3 intMul = A * 4;4 //should is replaced with "a << 2".5 intMUL2 = 8 *A;6 //should is replaced with "a << 3".7 inttemp = A * 3;8 }9}


Correct:

1  Packageopt;2  Public classSmul {3  Public voidCalculateinta) {4 intMul = a << 2;5 intMUL2 = a << 3;6 inttemp = A * 3;7 //cannot convert8 }9}

Summary of Java Program Performance optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.