Wednesday, June 10, 2009

Java Performance Tunning - Compiled




Core Java:

1. StringBuilder is better than StringBuffer, though both are usually better than using string concatenation to build strings (when performing extensive String manipulation, replacing + with StringBuilder.append is likely recommended)

2. Declare method arguments final if they are not modified in the method. In general declare all variables final if they are not modified after being initialized or set to some value.

3. Declare methods private and/or final whenever that makes sense.

4. Use static final when creating constants

5. Use zero-length arrays instead of null

6. HashMap has slightly better performance than LinkedHashMap

7. HashSet has slightly better performance than LinkedHashSet

8. Replace strings and other objects with integer constants. Compare these integers by identity.

9. Use primitive data types instead of objects as instance variables.

10. Overwrite compareTo() method for object comparision rather comparing in the loop. ( Implementing compareTo/hashCode
http://www.javapractices.com/topic/TopicAction.do?Id=10
http://www.javapractices.com/topic/TopicAction.do?Id=28 )

11. Logging the contents of a Collection is simple (JDK 1.5 onwards)

Arrays.toString(myArray);
Arrays.deepToString(myObjectArray); //recursive
Arrays.asList(myArray).toString();

12. To determine if two String objects match exactly, you should almost always use the equals method, and not the == operator

13.Follow naming convention for distinguishing between local variables, arguments, and fields strictly. (Code review would be very productivity)

14. Good example on ArrayList vs Linked List

14.1 Appending elements to the end of a list has a fixed averaged cost for both ArrayList and LinkedList. For ArrayList, appending typically involves setting an internal array location to the element reference, but occasionally results in the array being reallocated. For LinkedList, the cost is uniform and involves allocating an internal Entry object.

14.2 Inserting or deleting elements in the middle of an ArrayList implies that the rest of the list must be moved. Inserting or deleting elements in the middle of a LinkedList has fixed cost.

14.3 A LinkedList does not support efficient random access

14.4 An ArrayList has space overhead in the form of reserve capacity at the end of the list. A LinkedList has significant space overhead per element.

14.5 Sometimes a Map structure is a better choice than a List.

Get some more good stuff in the following URL: http://java.sun.com/developer/JDCTechTips/2002/tt0910.html
http://www.pankaj-k.net/archives/2004/06/arraylist_versu.html http://www.informit.com/guides/content.aspx?g=java&seqNum=95
http://www.javapractices.com/topic/TopicAction.do?Id=65

Database:

1. Stay away from the database as much as possible.

2. Cache as much as feasible - an in-memory cache is better than an on-disk one, which in turn is better than a remote or a relational database.

3. Caching coarse-grained objects is better than caching fine-grained ones.


Project Health:

1. Load testing can provide the basis for: Comparing varying architectural approaches; Performance tuning; Capacity planning. (TBS)

2. Initially you should identify the probable performance and scalability based on the requirements. You should be asking about: numbers of users/components; component interactions; throughput and transaction rates; performance requirements. (TBS)

3.Performance measurements should be from presentation start to presentation completion, i.e. user clicks button (start) and information is displayed (completion). (TBS)

4. Select performance benchmarks and use them to quantify the scalability and determine performance targets and future performance improvements or degradations. Include all user types such as "information-gathering" visitors or "transaction" visitors in your benchmarks.

5. Technical Problem - Different Source For Tooling, Build Solution, RAD Solution, Framework Lib, Server and more important No One owns the whole productivity Experience if project is developed using many open source project / lib.

Java Web Tech:

1. Excessive use of custom tags may create unnecessary processing overhead.

2. Use the include directive where possible, as this is a compile-time directive as it is faster than the include action

3. Logging is more important than the performance saved by not logging.

4. There is little performance penalty to using an MVC architecture.

5. To ensure good performance use experienced J2EE builders and use proven design patterns.