java optimize code performance optimization study concluded -java

Every little optimization, add up to a great leap forward performance;
the contrary, every careless code, it probably accumulate maintenance of a disaster;

What is worth in java optimization, or local pay attention to it?

1. 'system.arraycopy ()' instead of replication is circulated through the array
'system.arraycopy ()' through the array than to replicate much faster cycle

2. Use the shift operation (bit operators) instead of multiplication and division (a power of 2 of the time)
the shift operator with high efficiency

Possible with sync blocks 3. Use synchronized, synchronization method less
synchronized keyword can be applied in the process level (coarse-grained locks) or the code block level (fine-grained locking). Unless it can determine an entire methods are needed to synchronize, otherwise make use of synchronized block, avoid those that do not require synchronization code synchronization also affected the efficiency of code execution.

4. For a constant string, with 'string' instead of 'stringbuffer'
to replace stringbuffer string, this string is determined if it will not change, which will reduce operating costs to improve performance.

5. If the length of the content can be estimated to be added to the bottom set of an array manner, the tools to specify the initial length;
such ArrayList, LinkedLlist, StringBuilder, StringBuffer, HashMap, HashSet like.
For example: stringbuffer constructor creates a default size (usually 16) of an array of characters. In use, if you exceed this size, it will re-allocate memory, creating a larger array and copy over the original array, then discard the old array. In most cases, you can specify size when creating stringbuffer, thus avoiding the automatic increase in capacity is not enough time to improve performance.

6. Try specified class, final process modifiers
with the final modifier class is not derived. In the Java core API, there are many examples of final applications, such as java.lang.String, the entire class are final. Specify the final qualifier for the class allows the class can not be inherited, designated as ways to make the final modifier methods can not be overridden. If a class as final, the class of all methods are final. Java compiler will look for opportunities within the final with all methods, inline significant role for enhancing the operational efficiency of Java, refer to the specific Java runtime optimization. This can improve the performance by an average of 50%.

7. Use string splicing StringBuffer
Note the use of the String object, if not the same string, the use of String constant pool will be created, this is very resource-intensive. It should be used StringBuilder / StringBuffer place Therefore, if there is connected the string

8. The use of local variables as
parameters passed when calling the method and creates temporary variables are stored in the call stack in faster, other variables, such as static variables, instance variables, etc., are created in the heap, slow . In addition, the stack variables created with the method of operation is completed, the content is gone, no additional garbage collection.

9. Use a database connection pooling and thread pooling
the two pools are objects for reuse, the former can avoid frequent opening and closing connections, which can avoid frequently create and destroy threads

10. The basic data types into a string using .toString () manner
basic data types .toString () is the fastest way, String.valueOf (data), followed by data + "" slowest

11. The resources of close () is recommended separate operations
such as:

try{
XXX.close();
YYY.close();
}catch (Exception e)
{
...
}

Can be changed to:

try{ XXX.close(); }catch (Exception e) { ... }try{ YYY.close(); }catch (Exception e) { ... }

So although some trouble, but you can avoid resource leaks. We want, if not modified code, in case XXX.close () throw an exception, then entered the block in the cath, YYY.close () will not be executed, YYY this resource will not be recovered until occupied with such a code is more than one, is likely to cause resource handle leak. But after the following wording be changed, we ensure that in any case will be XXX and YYY close out.

Partner may feel nice little upper right corner a praise or attention yo!

Guess you like

Origin blog.csdn.net/fallwind_of_july/article/details/93628214