Generally speaking, avoid creating short-term temporary objects if you can. Fewer objects created mean less-frequent garbage collection, which has a direct impact on user experience.
Performance Myths
It was cheaper to invoke methods on a HashMap map than a Map map,
Prefer Static Over Virtual
If you don't need to access an object's fields, make your method static. Invocations will be about 15%-20% faster. It's also good practice, because you can tell from the method signature that calling the method can't alter the object's state.
Use Static Final For Constants
We can improve matters with the "final" keyword: static final String strVal = "Hello, world!"; instead of static String strVal = "Hello, world!";
strVal will use a relatively inexpensive "string constant" instruction instead of a field lookup.
Use Enhanced For Loop Syntax
Algorithm Performance C > B > A. Ex: A. 在for loop用length. B. 不在for loop用length, 用local variable存length. C. 用Foo a : mArray.
Avoid Enums Where You Only Need Ints
Use Package Scope with Inner Classes
Use Floating-Point Judiciously
Know And Use The Libraries
Ex: String.indexOf
Use Native Methods Judiciously
Use StringBuilder to improve string editing performance.
沒有留言:
張貼留言