Some skills of java code

1. Minimize repeated calculation of variables ...
1. Minimize repeated calculation of variables

A clear concept is that calling a method, even if there is only one sentence in the method, will consume, including creating a stack frame, protecting the site when calling the method, restoring the site when calling the method, etc. So, for example, the following operations:

for (int i = 0; i < list.size(); i++){ ... }

It is suggested to replace with:

for (int i = 0, length = list.size(); i < length; i++){ ... }
2. Multiplication and division use shift operations

For example:

for (val = 0; val < 100000; val += 5){ a = val * 8; b = val / 2; }

Using shift operation can greatly improve the performance, because the operation of bit alignment is the most convenient and fastest at the bottom of the computer. Therefore, it is recommended to modify it to:

for (val = 0; val < 100000; val += 5){ a = val << 3; b = val >> 1; }

Although the shift operation is fast, it may make the code difficult to understand, so it is best to add corresponding comments.

3. Collections that implement the RandomAccess interface, such as ArrayList, should be traversed using the most common for loop rather than a foreach loop

This is recommended by JDK to users. The JDK API explains the RandomAccess interface as follows: the RandomAccess interface is implemented to show that it supports fast random access. The main purpose of this interface is to allow general algorithms to change their behavior, so that it can provide good performance when applied to random or continuous access lists. Practical experience shows that if the class instance implementing RandomAccess interface is accessed randomly, the efficiency of using ordinary for loop will be higher than that of using foreach loop; Conversely, if it is accessed sequentially, it is more efficient to use Iterator.

The underlying implementation principle of foreach loop is Iterator, so the latter half of the sentence "if it is accessed sequentially, it will be more efficient to use Iterator" means those class instances accessed sequentially, and it is more efficient to use foreach loop to traverse;

A code similar to the following can be used for judgment:

if (list instanceof RandomAccess){ for (int i = 0, length = list.size(); i < length; i++){ ... } }else{ Iterator<?> iterator = list.iterable(); while (iterator.hasNext()){ iterator.next() } }
4. Convert a basic data type into a string. The basic data type. toString() is the fastest way, string. Valueof (data) is the second, and data + "" is the slowest

There are generally three ways to convert a basic data type into a string. For example, for an Integer type data I, you can use i.toString(), String.valueOf(i) and I + "". See a test for the efficiency of the three methods:

public static void main(String[] args){ int loopTime = 50000; Integer i = 0; long startTime = System.currentTimeMillis(); for (int j = 0; j < loopTime; j++){ String str = String.valueOf(i); } System.out.println("String.valueOf(): " + (System.currentTimeMillis() - startTime) + "ms"); startTime = System.currentTimeMillis(); for (int j = 0; j < loopTime; j++){ String str = i.toString(); } System.out.println("Integer.toString(): " + (System.currentTimeMillis() - startTime) + "ms"); startTime = System.currentTimeMillis(); for (int j = 0; j < loopTime; j++){ String str = i + ""; } System.out.println("i + \"\": " + (System.currentTimeMillis() - startTime) + "ms"); }

The operation result is:
String.valueOf(): 4ms
Integer.toString(): 3ms
i + "": 9ms

Therefore, when converting a basic data type to String in the future, the toString() method is preferred. As for why:

  • 1. The Integer.toString() method is called at the bottom of the String.valueOf() method, but short judgment will be made before calling;
  • 2. The Integer.toString() method will not be mentioned, but will be called directly;
  • 3. The i + "" bottom layer is implemented by StringBuilder. First, use the append method to splice, and then use the toString() method to obtain the string;

In contrast, 2 is the fastest, 1 is the second, and 3 is the slowest

5. The string constant is written in front of the string variable and the string constant equals

This is a common trick. If you have the following code:

String str = "123"; if (str.equals("123")) { ... }

It is suggested to amend to read:

String str = "123"; if ("123".equals(str)){ ... }

This is mainly to avoid null pointer exceptions;

The reference blog address is not recorded... If the original author sees it, please contact me~

Some optimization techniques encountered later will be continuously updated ~ [if you remember]

30 October 2021, 12:18 | Views: 6000

Add new comment

For adding a comment, please log in
or create account

0 comments