2011年1月28日 星期五

[Java] Performance Tuning

Performance Tuning:

  • Know How:
    • Designing for Performance
      • Avoid Creating Objects.
        • 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.
    • 不再使用的物件要盡早設定為 null,以便早點被當成垃圾清掉。
    • Use global variable to reuse memory.
  • Reference:

沒有留言:

張貼留言