2011年5月12日 星期四

Memo: Java Programming with Performance in mind

This memo make a comment on "Java Programming with Performance in mind"

The only problem is I don't know when this article published. Because some people tell me, the String object in newly JDK is improved, therefore some issue may already adjust.

1. Primitive is fast then Object
2. String is slow, use StringBuilder when no synchronization needs, use StringBuffer when requiring synchronization.
3. Some JDK object will do some work then return copies, if you need the copiess' service, you should use that copies instead of requiring another return from object.
4. choose the right collection
(sec) ArrayList LinkedList HashSet HashMap Vector Hashtable
Add 0.11 0.23 0.63 0.62 0.11 0.37
Get 0.01 160 0.03 0.07 0.01 0.06
Delete 5.52 0.02 0.13 0.13 5.57 0.08

Vector and Hashtable is synchronized by default. ArrayList can be synchronized by using Collections.synchronizedList( new ArrayList()).
5. System.arraycopy is fast then your code
6. Arrays.equals(), fill(), sort() is fast then your code
7. try is not free, throw is expensive; good logic is fast then exceptions
8. thread synchronization: synchronized only for critical section
9. writing to console window: hidding console when not needed
10. Accessing SQL DB using JDBC: tip> use PreparedStatement, combining related SQL in a single JDBC transaction.
11. perceived performance: byte code reduction> remove unused code, compressed Jar file, obfuscate