Sponsored Link •
If, despite the pleadings of your peers and the advice of software heavyweights such as Donald Knuth, you decide to design your Java program for performance rather than maintainability, here's the popular lore that explains how to do it:
The first three guidelines arise from the expense of dynamic method dispatch. Because dynamic method dispatch is expensive in current JVMs, the lore says, designing programs that have fewer dynamic method invocations at run time should boost performance. Making methods static, which doesn't require dynamic dispatch, and final, which can enable the JVM to optimize out the dynamic dispatch, should help performance.
In addition, simply favoring large methods over small ones can help reduce the number of dynamic method dispatches, because small methods would need to invoke each other more often. Likewise, preferring large classes over small ones could reduce the number of dynamic method dispatches, because objects communicate and cooperate with each other by invoking each other's methods.
The fourth guideline arises out of the relative expense of interface method invocations. Because interface method invocation is a lot slower than regular dynamic method dispatch in current JVMs, the lore says that avoiding the use of interfaces should help boost performance.
The fifth guideline attempts to address the cost of object allocation in current JVMs. Each object allocation exacts some time cost, and eventually objects may need to be garbage collected. In light of these costs, the lore advises you to avoid creating a lot of objects -- especially short-lived, temporary objects.
The sixth guideline arises out of the cost of invoking a synchronized method as compared to a non-synchronized method. In current JVMs, synchronization is very expensive. Thus, the lore says you should avoid using synchronization altogether.
Although you may see some performance improvements by following the guidelines listed above, you will almost certainly make your code harder to read and maintain. Is the tradeoff worth it?
Well, one thing to keep in mind is that Sun's next-generation virtual machine is aimed directly at the problems the lore's guidelines are trying to address. Hotspot tackles each performance problem of current JVMs. In the process, it should eliminate the "performance excuse" for creating poor designs and writing bad code.