Sunday, January 4, 2015

Java Performance

This post is about performance factors in java. There are many performance factors in java. Here i am mentioning some of the performance factors which i have used in early days in java. I will keep posted some approach, experience on performance improvement.

some of the performance tips are,

  1. ArrayList is faster than Vector except when there is no lock acquisition required.
  2. Use StringBuffer in place of String if you are doing lots of string manipulation it will reduce memory by avoiding creating lots of string garbage. If you are using java5 then consider StringBuilder but that is not synchronized so beware.
  3. Try to make variable , class , method final whenever possible that’s allow compiler to do lots of optimization e.g. compile time binding so you will get faster output.
  4. Static methods are bonded compile time while non static methods are resolved at runtime based on object type so static method will be faster than non static.
  5. Don't call methods in for loop for checking condition e.g. length() size() etc. instead of doing this , use modified version.
  6. Use exceptions only where you really need them
  7. Local variables are the faster than instance variables, which are in turn faster than array elements
  8. Use the fastest available JVM
  9. '==' is faster than equals()
  10. Creating Doubles from strings is slow
  11. Combine similar loops
  12. Don't create too may objects
  13. Beware of object leaks (references to objects that are never nulled)
  14. Use Lazy loading to avoid unnecessary pre-loading of child data
  15. Do bulk updates to reduce database calls
  16. Use the print() method rather than the println() method

No comments:

Post a Comment