Here is my experience in improving program performance during Java development. If my experience is incorrect or you have good optimization experience, you can make a comment here, so that you can have a more complete and effective discussion on optimizing Java programs. Thank you.
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.
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 ".
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. For example, if Sb references an instance of stringbuilder, SB. append (X) and SB. insert (sb. Length (), x) have the same effect. 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.
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. When determining the number of cycles, try to use the for loop. In the nested loop, try to put the large loop to the outer layer, and the small loop to the inner layer. This is exactly the opposite of other languages such as VB, which have been tested by code, but I still don't know why. In short, it is much faster to use small sets in Microsoft series languages such as VB, and to use large sets of small sets in Java! Of course, this means that the two can replace each other, such as two-dimensional arrays!
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.
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.
In terms of session usage, try not to place large objects into httpsession or other objects to be serialized, and clear the session in time.