Item 67: Optimize judiciously

67. Optimization should be done with care

Aim for a better program than a fast program

There is a long-standing saying that encourages program optimization not to be easy.

Do not destroy a healthy architecture for optimization. If a healthy architecture is maintained, the individual components should be loosely related and can be changed later without affecting others.

That doesn't mean you don't have to think about performance until the program is complete. It is almost impossible to fix flaws in popular architectures. Therefore, it is necessary to give sufficient consideration to performance at the design stage.

Avoid designs that limit performance

When designing a component, the most difficult thing to change once it is decided is the boundary between the outside world and the component. The main example is

Think about performance in API design

In API design, we have to think about performance.

As an example, the getSize method of the java.awt.Component class is supposed to generate a mutable Dimension interface each time it is called, which will have a performance impact.

Originally, Dimension should have been immutable (Item17). Also, the getSize method should have been replaced by two methods that return components for individual Dimension objects (it doesn't come). In fact, the Component class has such a method in Java2, but code that has been using getSize for some time still has performance issues.

In general, good API design comes with good performance. ** It's awkward to distort the API to improve performance. ** ** The performance gains gained there can be wiped out by future platform changes, but you'll still have the difficulty of maintaining a distorted API.

optimisation

If performance improvement is still needed after successful design and implementation, try optimization.

Measure performance

Performance measurements need to be taken before and after optimization. Since it is difficult to predict which program is taking a long time to execute, we will proceed with optimization while measuring so as not to waste time.

Profiling tool

By using the profiling tool, you can see which method takes how long and how many times it is called. At this time, you can see if the algorithm selection is correct. The choice of algorithm can dramatically change performance, so make it appropriate.

There is also a tool called JMH, which is a micro-benchmarking framework. This allows you to visualize detailed performance information in your Java code.

The need for measurement in Java

Performance measurement in Java is more necessary than in older languages such as C and C ++. This is because there is a large gap between the processing executed by the CPU and what is expressed in the code compared to those languages.

Also, in Java, performance varies depending on the implementation, release version, and processor. If you find that it runs on different platforms, it's important to measure performance in each environment. As a result, performance trade-offs for each environment often occur.

As the environment in which Java runs is becoming more complex, performance is becoming less predictable. Therefore, it can be said that the need for measurement is increasing.

Recommended Posts

Item 67: Optimize judiciously
Item 55: Return optionals judiciously
Item 52: Use overloading judiciously
Item 53: Use varargs judiciously
Item 45: Use streams judiciously
Item 83: Use lazy initialization judiciously
Item 66: Use native methods judiciously
Item 32: Combine generics and varargs judiciously