The essence of optimization
The purpose of source code optimization of marriage and love system is to ensure the robustness of the program. Sometimes it is not necessary to spend too much time when the program can run normally.
Cyclic optimization
Take an example, please pay attention to this code, the source code of marriage and marriage system invoked the method in the rotation, so getList() will perform multiple times, resulting in performance loss.
public static void main(String[] args) { for (int i = 0; i < getList().size(); i++) { } } public static List<String> getList() { System.out.println("test"); List<String> stringList = new ArrayList<>(); stringList.add("1"); stringList.add("2"); stringList.add("3"); return stringList; }
Even if getList() is stored as a temporary variable, the size() method will still be executed many times, for example
public static void main(String[] args) { List<String> list = getList(); for (int i = 0; i < list.size(); i++) { } }
Therefore, the correct implementation method of the marriage and love system source code is
public static void main(String[] args) { for (int i = 0, size = getList().size(); i < size; i++) { } }
The example of appeal proves a point
Never use a method call on the second argument of a for loop
Try to use local variables
When the local variables of the marriage and love system source code are used up, they are in the invisible stage, and the garbage collection mechanism will recycle them as soon as possible.
Use StringBuilder and StringBuffer instead of String
Although these three objects can realize the function of splicing strings, they will have great performance order, because the String itself exists in the constant pool. Any change to the String will not affect the original object, and any related change will generate new objects.
Let's see the difference between them.
- StringBuilder is asynchronous, thread unsafe and most efficient.
- StringBuffer is thread safe and its efficiency is lower than that of StringBuilder. There is no need to lock the operation in multiple threads;
- String is stored in the constant pool, which is the slowest and cannot be changed frequently.
To verify their efficiency.
public static void main(String[] args) { long times = System.currentTimeMillis(); String string = ""; for (int i = 0; i < 10000; i++) { string = string + "a"; } System.out.println("Interval:" + (System.currentTimeMillis() - times)); // 304 }
public static void main(String[] args) { long times = System.currentTimeMillis(); StringBuffer stringBuffer = new StringBuffer(); for (int i = 0; i < 10000; i++) { stringBuffer.append("a"); } System.out.println("Interval:" + (System.currentTimeMillis() - times)); // 20 }
public static void main(String[] args) { long times = System.currentTimeMillis(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < 10000; i++) { stringBuilder.append("a"); } System.out.println("Interval:" + (System.currentTimeMillis() - times)); // 15 }
I tried to splice strings ten thousand times. From the conclusion, there is a huge gap between StringBuild and String.
Release objects in time
In fact, the recycling mechanism will automatically recycle some objects that are no longer used in the source code of the marriage and love system. Generally, we don't need to recycle manually. We can only suggest / remind it to recycle. What do you mean?
We can assign Null to this object.
if(obj == null) create obj; ... obj = null;
The above is where the love and marriage system source code can be optimized.
Statement: This article is forwarded by cloudleopard technology from Android Tangfu blog. If there is infringement, please contact the author to delete it