“This is the 23rd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Android Java Code Optimization (1)

Since kotlin is a native Language for Android, and this article is about Java code optimization, let’s distinguish the title ‘Java Code optimization’. This article focuses on notable optimization in programming.

The nature of optimization

The goal of optimization is to keep your program robust, and when it works, sometimes it doesn’t take much time.

Loop optimization

For example, notice code that calls a method in a loop, so getList() is executed multiple times, resulting in a performance penalty.

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;
    }
Copy the code

The size() method is executed multiple times even though getList() is stored as a temporary variable, for example

 public static void main(String[] args) {
        List<String> list = getList();
        for (int i = 0; i < list.size(); i++) {

        }
    }
Copy the code

So the correct way to do it is

 public static void main(String[] args) {
        for (int i = 0, size = getList().size(); i < size; i++) {

        }
    }
Copy the code

The example of appeal proves a point

Never use a method call on the second argument of a for loop

Use local variables whenever possible

Local variables are invisible when they are used up, and the garbage collection mechanism collects them as quickly as possible.

Use StringBuilder and StringBuffer instead of String

Although all three objects can concatenate strings, they have a significant performance order, because the String itself is stored in the constant pool, and any changes it makes to the original object do not affect the original object, and any related changes will generate new objects.

Let’s see the difference between them.

  • Stringbuilders are asynchronous, thread-unsafe, and most efficient.
  • A StringBuffer is thread-safe and less efficient than a StringBuilder.
  • String is stored in the constant pool, which is the slowest, and frequent changes are not desirable.

Let’s see how efficient they are.

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}Copy the code
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}Copy the code
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}Copy the code

I’ve tried to concatenate strings 10,000 times, and I can see that StringBuild is a far cry from String.

Releasing objects in time

In fact, the recycle mechanism will automatically recycle some objects that are no longer used. In general, we do not need to manually recycle, we can only suggest/remind it to recycle. What does that mean?

We just assign Null to this object.

if(obj == null) create obj; . obj = null;Copy the code