Reprinted from www.matrix.org.cn
I don't know if anyone has done this before. Today, I am opening my head. I hope you can post your experiences and discuss them together, which will be of great help to write highly efficient code.
I am here to start
Here we will compare arraylist and rule list:
1. arraylist is based on arrays, and arraylist is implemented based on linked lists.
2. for Random Access to get and set, arraylist thinks it is better than the sorted list because the sorted list needs to move the pointer.
3. For add and remove operations, linedlist is dominant because arraylist needs to move data.
4. Search for indexof, lastindexof, and contains. The two are similar.
This is only a theoretical analysis. In fact, it is not necessarily true. For example, if arraylist inserts or deletes data at the end, it is not designed to move data.
We recommend that you use arraylist instead of rule list for random access. If you need frequent insertion or deletion
Consider using the consumer list to improve performance.
Multiple Dimension Array
In Java, the multiple dimension array is actually simulated using array of arrays. As a result, they may not be stored in contiguous memory space, which may cause poor memory access.
The ninja group of IBM has developed a package to solve this problem. array package
Reducing unnecessary object creation is a good way to improve performance.
Not only for Java, but for all languages.
Avoid using complex expressions in loop conditions without compilation optimization. in a loop, loop conditions are calculated repeatedly. If complex expressions are not used, if the cyclic condition value remains unchanged, the program runs faster.
When defining the initial size for 'vectors 'and 'hashtables', JVM needs to re-create a larger array to copy the content of the original array. Finally, the original array is recycled. It can be seen that the expansion of vector capacity is quite time-consuming. Generally, the size of the default 10 elements is insufficient. You 'd better accurately estimate the optimal size you need.
Resources used in stream program disabling in finally blocks should be released to avoid resource leakage. This should be done in finally blocks. Regardless of the execution result of the program, the Finally block is always executed to ensure that the resource is properly closed.
Use 'System. arraycopy () 'instead of repeating the Array
'System. arraycopy () 'is much faster than copying arrays through loops.
Use short or even byte in some cycles that know the number of loops.
As if many people are used to int
Use stringbuffer whenever possible for string connection
Variable Character Sequence for Ava. Lang. stringbuffer thread security. A string buffer similar to a string, but cannot be modified. Although it contains a specific character sequence at any time point, the length and content of the sequence can be changed by calling some methods.
The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary, so all the operations on any specific instance are in serial order, this sequence is consistent with the method call sequence of each involved thread.
The main operations on stringbuffer are append and insert methods. You can reload these methods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string into the string buffer. The append method always adds these characters to the end of the buffer, while the insert method adds the characters at the specified point.
For example, if z references a string buffer object whose current content is "start", this method calls Z. append ("Le") causes the string buffer to contain "startle", while Z. insert (4, "Le") will change the string buffer to include "starlet ".
Generally, if Sb references an instance of stringbuilder, SB. append (X) and SB. insert (sb. Length (), x) have the same effect.
If an operation is performed on the source sequence (such as append or insert in the source sequence), the operation is only performed on the string buffer for this operation, rather than on the source.
Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. From JDK 5, a equivalence class used by a single thread is added for this class, that is, stringbuilder. Compared with this class, the stringbuilder class should be used first, because it supports all the same operations, but because it does not execute synchronization, It is faster.
Java. Lang. stringbuilder a variable character sequence. This class provides a stringbuffer-compatible API, but does not guarantee synchronization. This class is designed as a simple replacement of stringbuffer, used when the string buffer is used by a single thread (this is common ). If possible, we recommend that you use this class first, because in most implementations, It is faster than stringbuffer.
The main operations on stringbuilder are append and insert methods. You can reload these methods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string to the string generator. The append method always adds these characters to the end of the generator, while the insert method adds the characters at the specified point.
For example, if z references a string generator object whose current content is "start", this method calls Z. append ("Le") will make the string generator contain "startle", while Z. insert (4, "Le") changes the string generator to include "starlet ".
Generally, if Sb references the stringbuilder instance, SB. append (X) and SB. insert (sb. Length (), x) have the same effect. Each string generator has a certain capacity. As long as the length of the Character Sequence contained in the string generator does not exceed this capacity, no new internal buffer needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically.
It is not safe to use stringbuilder instances for multiple threads. If such synchronization is required, stringbuffer is recommended.
Minimize the accessibility of classes and members
Combination takes precedence over inheritance
The interface takes precedence over abstract classes.
If the character string is particularly long, it is very time-consuming to use charat to obtain characters at specific positions one by one. Because each time you obtain the characters at the specified index location, a new search process is required. A better way is to convert the string to a character array by calling the tochararray method, then, the characters at the specified position are obtained through the array index value.
Replacing the 'A/B 'operation with the shift operation is an expensive operation. Using the shift operation will be faster and more effective.
For Boolean values, it is not necessary to determine the equality between a Boolean value and a true value as a constant operation (directly return the value of this Boolean variable ). removing unnecessary operations on Boolean brings at least two benefits: 1) Faster Code Execution (5 bytes less generated); 2) Cleaner code
Do not place large objects in httpsession or other objects to be serialized, and clear the session in time.
The following is a reference to code Daquan:
I learned a lesson. If performance changes are not measured, the optimization result you take for granted is nothing more difficult than code. If you do not think it is necessary to determine which method is more effective through measurement, there is no need to sacrifice the readability of the code and place the bet on performance improvement.
We should not adopt a more obscure approach to performance improvement.
For example, "using shift operations to replace 'a/B 'operations"/"on the 14th floor is a very expensive operation. Using shift operations will be faster and more effective ."
First, what is the performance loss?
Is the performance within the acceptable range?
What's more, the high-performance method is adopted, but the ultimate performance improvement is almost negligible.
I did a test on the above operation: In the 0.1 million operation, the integer division operation that can be converted to the shift operation took 16 milliseconds .. Is it necessary to optimize the code?
Code 1: Run Time 16 milliseconds
Long start = system. currenttimemillis ();
For (INT I = 0; I <100000; I ++ ){
Long n = Start/2;
}
System. Out. println (system. currenttimemillis ()-Start );
Code 2: running time 0
Long start = system. currenttimemillis ();
For (INT I = 0; I <100000; I ++ ){
Long n = Start> 1;
}
System. Out. println (system. currenttimemillis ()-Start );
Many classes in Java are set for synchronization. For example, hashtable makes some good designs to prevent problems during simultaneous access by multiple users. If your data can be accessed by multiple people, but it is written at a time, and then basically the value has read operations, you can consider not using its synchronization mechanism, instead, use hashmap instead. And do some control in the appropriate place. (Not recommended specially)
String/stringbuffer/stringbuilder, which improves the performance of string connection operations in turn. I have conducted an experiment to perform 10000 consecutive string operations. The speed gap between the three operations is astonishing. It is best to use string when you know exactly whether it is a constant string, because in Java, string adopts the flyweight mode, and only one identical string exists in JVM.
Replace I ++ with ++ I