Item 55: Optimize judiciously

1.  Don’t sacrifice sound architectural principles for performance. Strive to write good programs rather than fast ones.

 

2.  Implementation problems can be fixed by later optimization, but pervasive architectural flaws that limit performance can be impossible to fix without rewriting the system. Therefore you must think about performance during the design process.

 

3.  The components of a design that are most difficult to change after the fact are

those specifying interactions between modules and with the outside world. Chief among these design components are APIs, wire-level protocols, and persistent data formats. Not only are these design components difficult or impossible to change after the fact, but all of them can place significant limitations on the performance that a system can ever achieve.

 

4.  It is generally the case that good API design is consistent with good performance. It is a very bad idea to warp an API to achieve good performance. The performance issue that caused you to warp the API may go away in a future release of the platform or other underlying software, but the warped API and the support headaches that come with it will be with you for life.

 

5.  Once you’ve carefully designed your program and produced a clear, concise, and well-structured implementation, then it may be time to consider optimization, assuming you’re not already satisfied with the performance of the program. Measure performance before and after each attempted optimization.

 

6.  The first step of optimization is to examine your choice of algorithms: no amount of lowlevel optimization can make up for a poor choice of algorithm.

 

7.  The need to measure the effects of attempted optimization is even greater on the Java platform than on more traditional platforms, because the Java programming language does not have a strong performance model. The relative costs of the various primitive operations are not well defined.

 

8.  If you will be running your program on multiple JVM implementations or multiple hardware platforms, it is important that you measure the effects of your optimization on each. Occasionally you may be forced to make trade-offs between performance on different JVM implementations or hardware platforms.

 

你可能感兴趣的:(performance,optimization)