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 |
int
loopTime =
50000
;
Integer i =
0
;
long
startTime = System.currentTimeMillis();
for
(
int
j =
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 |
int
loopTime =
50000
;
Integer i =
0
;
long
startTime = 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