Java Performance optimization

Source: Internet
Author: User

10 Simple Java Performance optimizations:

1. Using StringBuilder

2. Avoid using regular expressions

With regular expressions, you should at least cache the pattern to avoid compiling the pattern repeatedly.

Static final Pattern Heavy_regex =

Pattern.compile (((((*y) *z) * ");

3. Do not use the iterator () method

4. Do not call the high overhead method

5. Using the original type and stack

Stored on the heap

Integer i = 817598;

Stored on the stack

int i = 817598;

6. Avoid recursion

7. Use EntrySet () to iterate map

8. Use Enumset or Enummap

When using the configuration map, we may know the key values stored in the map in advance. If this key value is very small, we should consider using Enumset or Enummap

9. Optimize the custom Hascode () method and the Equals () method

10. Consider using set instead of a single element

11, do not use in the loop try...catch ..., should be placed on the outermost layer, in the execution of the exception will continue to loop. /

12. Do not constantly create object references within the loop,/

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

13, as far as possible to avoid arbitrary use of static variables, GC usually does not reclaim the heap memory of this object

14. Declare a constant as static final and name it in uppercase

15, string variable and string constant equals when the string constant is written in front, the variable is written in front, and Null is written in the back.

16. To convert a basic data type to a string, the base data type. ToString () is the quickest way, string.valueof (data), Data + "" slowest

17. The collection must specify the template type (5.0+). Description: Easy to read the program, eliminate the forced conversion code

18, abnormal capture as far as possible do not direct catch (Exception ex), should be the exception of the subdivision processing. Description: Can design more reasonable exception processing branch, troubleshooting.

19, more public methods to extract, do not repeat one thing.

1234567 intloopTime = 50000;Integer i = 0;longstartTime = System.currentTimeMillis();for(intj = 0; j < loopTime; j++){String str = String.valueOf(i);}System.out.println("String.valueOf():"+ (System.currentTimeMillis() - startTime) + "ms");

10 Simple Java Performance optimizations:

1. Using StringBuilder

2. Avoid using regular expressions

With regular expressions, you should at least cache the pattern to avoid compiling the pattern repeatedly.

Static final Pattern Heavy_regex =

Pattern.compile (((((*y) *z) * ");

3. Do not use the iterator () method

4. Do not call the high overhead method

5. Using the original type and stack

Stored on the heap

Integer i = 817598;

Stored on the stack

int i = 817598;

6. Avoid recursion

7. Use EntrySet () to iterate map

8. Use Enumset or Enummap

When using the configuration map, we may know the key values stored in the map in advance. If this key value is very small, we should consider using Enumset or Enummap

9. Optimize the custom Hascode () method and the Equals () method

10. Consider using set instead of a single element

11, do not use in the loop try...catch ..., should be placed on the outermost layer, in the execution of the exception will continue to loop. /

12. Do not constantly create object references within the loop,/

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

13, as far as possible to avoid arbitrary use of static variables, GC usually does not reclaim the heap memory of this object

14. Declare a constant as static final and name it in uppercase

15, string variable and string constant equals when the string constant is written in front, the variable is written in front, and Null is written in the back.

16. To convert a basic data type to a string, the base data type. ToString () is the quickest way, string.valueof (data), Data + "" slowest

17. The collection must specify the template type (5.0+). Description: Easy to read the program, eliminate the forced conversion code

18, abnormal capture as far as possible do not direct catch (Exception ex), should be the exception of the subdivision processing. Description: Can design more reasonable exception processing branch, troubleshooting.

19, more public methods to extract, do not repeat one thing.

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

Java Performance optimization

Related Article

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.