1. 移位操作可以大大提高效率。使用移位操作来计算乐观整数2^n(n是乐观整数)的乘除。
Inefficient code: int num1 = a * 4; int num2 = a / 4;
Environment friendly code: int num1 = a << 2; int num2 = a >> 2;
|
2. 类中每个字段变量在每个对象中都有一个复制,而静态固定的每个字段则只有一个。
Inefficient code: public class HttpConnection { non-public remaining lengthy timeout = 5L; ... }
Environment friendly code: public class HttpConnection { non-public static remaining lengthy TIMEOUT = 5L; ... }
|
3.只要您直接分配数据值时,就只要创建一个对象引用直接赋值即可。
Inefficient code: Lengthy i = new Lengthy(1L); String s = new String("abc");
Environment friendly code: Lengthy i = 1L; String s = "abc";
|
4.在for循环的情况下使用方法调用会浪费资源。
Inefficient code: Listing<UserDO> userList = ...; for (int i = 0; i < userList.dimension(); i++) { ... }
Environment friendly code: Listing<UserDO> userList = ...; int userLength = userList.dimension(); for (int i = 0; i < userLength; i++) { ... }
|
5. 除非!至关重要,否则不要使用。在non-operator区域会再一次计算
Inefficient code: if (!(a >= 10)) { ... // Your Code } else { ... // Your Code }
Environment friendly code: if (a < 10) { ... // Your Code } else { ... // Your Code }
|