Java Reference
In-Depth Information
Optimize for the Common Case
It is tempting—particularly given the “death by 1,000 cuts” syndrome—to treat all perform-
ance aspects as equally important. But focus should be given to the common use case scen-
This principle manifests itself in several ways:
▪ Optimize code by profiling it and focusing on the operations in the profile taking the
most time. Note, however, that this does not mean looking at only the leaf methods in a
profile (see Chapter 3 ) .
▪ Apply Occam's Razor to diagnosing performance problems. The simplest explanation for
a performance issue is the most conceivable cause: a performance bug in new code is
more likely than a configuration issue on a machine, which in turn is more likely than a
bug in the JVM or operating system. Obscure bugs do exist, and as more credible causes
for a performance issue are ruled out, it does become possible that somehow the test case
in question has triggered such a latent bug. But don't jump to the unlikely case first.
▪ Write simple algorithms for the most common operations in an application. Take the case
of a program that estimates some mathematical formula, where the user can decide if she
wants an answer within a 10% margin of error, or a 1% margin. If most users will be sat-
isfied with the 10% margin, then optimize that code path—even if it means slowing
down the code that provides the 1% margin of error.
Java 7 and 8 introduce a number of new features and tools that make it even easier to get the
best possible performance from a Java application. This topic should help you understand
how best to use all the features of the JVM in order to end up with fast-running programs.
In many cases, though, remember that the JVM is a small part of the overall performance
picture. A systemic, system-wide approach to performance is required in Java environments
where the performance of databases and other backend systems is at least as important as the
performance of the JVM. That level of performance analysis is not the focus of this topic—it
is assumed the due diligence has been performed to make sure that the Java component of
the environment is the important bottleneck in the system.
However, the interaction between the JVM and other areas of the system is equally import-
ant—whether that interaction is direct (e.g., the best way to use JDBC) or indirect (e.g., op-
Search WWH ::

Custom Search